Browse Source

Better structs, use constants

Volodymyr Tkach 2 years ago
parent
commit
843941e7d6
4 changed files with 27 additions and 14 deletions
  1. 1 1
      pubsub/connection.go
  2. 2 2
      pubsub/go_pinger.go
  3. 7 6
      pubsub/go_reader.go
  4. 17 5
      pubsub/structs.go

+ 1 - 1
pubsub/connection.go

@@ -117,7 +117,7 @@ func (c *Connection) listenTopis() {
 	for topic := range c.topics {
 		topics = append(topics, topic)
 	}
-	msg := Response{Type: "LISTEN", Data: DataTopics{Topics: topics}}.JSON()
+	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

+ 2 - 2
pubsub/go_pinger.go

@@ -17,7 +17,7 @@ func go_pinger(c *Connection) {
 					if time.Since(c.ping_start) > TwitchApiPingEach {
 						if err := c.Connection.WriteMessage(
 							websocket.TextMessage,
-							Response{Type: "PING"}.JSON(),
+							Answer{Type: Ping}.JSON(),
 						); err != nil {
 							c.onError(err)
 							c.active = false
@@ -42,7 +42,7 @@ func go_pinger(c *Connection) {
 			case <-time.After(1 * time.Second):
 				if c.active && c.ping_sended {
 					if time.Since(c.ping_start) > TwitchApiPingTimeout {
-						c.onInfo(fmt.Sprintf("warning, no PONG response more than %d seconds", TwitchApiPingTimeout))
+						c.onInfo(fmt.Sprintf("warning, no %s response more than %d seconds", Pong, TwitchApiPingTimeout))
 						c.active = false
 						c.onDisconnect()
 						c.ping_start = time.Now()

+ 7 - 6
pubsub/go_reader.go

@@ -2,6 +2,7 @@ package pubsub
 
 import (
 	"encoding/json"
+	"fmt"
 	"time"
 )
 
@@ -26,17 +27,17 @@ func go_reader(c *Connection) {
 							return
 						}
 					} else {
-						var resp Response
-						if err := json.Unmarshal(msg, &resp); err != nil {
+						var answer Answer
+						if err := json.Unmarshal(msg, &answer); err != nil {
 							c.onError(err)
 						} else {
-							if resp.Type == "PONG" {
+							if answer.Type == Pong {
 								ct := time.Now()
 								c.onPong(c.ping_start, ct)
 								c.ping_start = ct
 								c.ping_sended = false
-							} else if resp.Type == "RECONNECT" {
-								c.onInfo("warning, got RECONNECT response")
+							} else if answer.Type == Reconnect {
+								c.onInfo(fmt.Sprintf("warning, got %s response", Reconnect))
 								c.active = false
 								c.onDisconnect()
 								c.ping_start = time.Now()
@@ -44,7 +45,7 @@ func go_reader(c *Connection) {
 								if err := c.Connection.Close(); err != nil {
 									c.onError(err)
 								}
-							} else if resp.Type == "RESPONSE" {
+							} else if answer.Type == Response {
 								// TODO: {"type":"RESPONSE","error":"","nonce":""}
 							} else {
 								c.onMessage(msg)

+ 17 - 5
pubsub/structs.go

@@ -2,23 +2,35 @@ package pubsub
 
 import "encoding/json"
 
-type Response struct {
-	Type  string      `json:"type"`
+type AnswerType string
+
+const (
+	Listen    AnswerType = "LISTEN"
+	Message   AnswerType = "MESSAGE"
+	Ping      AnswerType = "PING"
+	Pong      AnswerType = "PONG"
+	Reconnect AnswerType = "RECONNECT"
+	Response  AnswerType = "RESPONSE"
+	Unlisten  AnswerType = "UNLISTEN"
+)
+
+type Answer struct {
+	Type  AnswerType  `json:"type"`
 	Data  interface{} `json:"data,omitempty"`
 	Error string      `json:"error,omitempty"`
 	Nonce string      `json:"nonce,omitempty"`
 }
 
-func (r Response) JSON() []byte {
+func (r Answer) JSON() []byte {
 	bytes, _ := json.Marshal(r)
 	return bytes
 }
 
-type DataTopics struct {
+type AnswerDataTopics struct {
 	Topics []string `json:"topics"`
 }
 
-func (d DataTopics) JSON() []byte {
+func (d AnswerDataTopics) JSON() []byte {
 	bytes, _ := json.Marshal(d)
 	return bytes
 }