Browse Source

Implement TopicsCount func

Volodymyr Tkach 2 years ago
parent
commit
710ef941ce
1 changed files with 19 additions and 10 deletions
  1. 19 10
      pubsub/connection.go

+ 19 - 10
pubsub/connection.go

@@ -131,11 +131,13 @@ func (c *Connection) listenTopis() {
 	}
 
 	// Send LISTEN request
-	msg := Answer{Type: Listen, Data: AnswerDataTopics{Topics: topics}}.JSON()
-	if err := c.Connection.WriteMessage(websocket.TextMessage, msg); err != nil {
-		c.onError(err)
-		c.active = false
-		c.onDisconnect()
+	if c.Connection != nil {
+		msg := Answer{Type: Listen, Data: AnswerDataTopics{Topics: topics}}.JSON()
+		if err := c.Connection.WriteMessage(websocket.TextMessage, msg); err != nil {
+			c.onError(err)
+			c.active = false
+			c.onDisconnect()
+		}
 	}
 }
 
@@ -169,11 +171,13 @@ func (c *Connection) RemoveTopic(topic string) {
 	delete(c.topics, topic)
 
 	// Send UNLISTEN request
-	msg := Answer{Type: Unlisten, Data: AnswerDataTopics{Topics: []string{topic}}}.JSON()
-	if err := c.Connection.WriteMessage(websocket.TextMessage, msg); err != nil {
-		c.onError(err)
-		c.active = false
-		c.onDisconnect()
+	if c.Connection != nil {
+		msg := Answer{Type: Unlisten, Data: AnswerDataTopics{Topics: []string{topic}}}.JSON()
+		if err := c.Connection.WriteMessage(websocket.TextMessage, msg); err != nil {
+			c.onError(err)
+			c.active = false
+			c.onDisconnect()
+		}
 	}
 
 	// No topics, close connection
@@ -203,6 +207,11 @@ func (c *Connection) Topics() []string {
 	return topics
 }
 
+// TopicsCount return count of topics.
+func (c *Connection) TopicsCount() int {
+	return len(c.topics)
+}
+
 // Close is close connection and shutdown all goroutines.
 // Usually it's need to call before destroying.
 func (c *Connection) Close() error {