Browse Source

Process RESPONSE

Volodymyr Tkach 2 years ago
parent
commit
68cee68e1f
2 changed files with 20 additions and 6 deletions
  1. 5 1
      pubsub/go_reader.go
  2. 15 5
      pubsub/structs.go

+ 5 - 1
pubsub/go_reader.go

@@ -46,7 +46,11 @@ func go_reader(c *Connection) {
 									c.onError(err)
 								}
 							} else if answer.Type == Response {
-								// TODO: {"type":"RESPONSE","error":"","nonce":""}
+								if answer.HasError() {
+									c.onError(fmt.Errorf(answer.Error))
+								} else {
+									c.onInfo(fmt.Sprintf("type: %s, data: %#v", answer.Type, answer.Data))
+								}
 							} else {
 								c.onMessage(msg)
 							}

+ 15 - 5
pubsub/structs.go

@@ -1,6 +1,8 @@
 package pubsub
 
-import "encoding/json"
+import (
+	"encoding/json"
+)
 
 type AnswerType string
 
@@ -14,6 +16,10 @@ const (
 	Unlisten  AnswerType = "UNLISTEN"
 )
 
+func (a AnswerType) String() string {
+	return string(a)
+}
+
 type Answer struct {
 	Type  AnswerType  `json:"type"`
 	Data  interface{} `json:"data,omitempty"`
@@ -21,8 +27,12 @@ type Answer struct {
 	Nonce string      `json:"nonce,omitempty"`
 }
 
-func (r Answer) JSON() []byte {
-	bytes, _ := json.Marshal(r)
+func (a Answer) HasError() bool {
+	return a.Error != ""
+}
+
+func (a Answer) JSON() []byte {
+	bytes, _ := json.Marshal(a)
 	return bytes
 }
 
@@ -30,7 +40,7 @@ type AnswerDataTopics struct {
 	Topics []string `json:"topics"`
 }
 
-func (d AnswerDataTopics) JSON() []byte {
-	bytes, _ := json.Marshal(d)
+func (a AnswerDataTopics) JSON() []byte {
+	bytes, _ := json.Marshal(a)
 	return bytes
 }