-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
65 lines (55 loc) · 1.48 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package bittrex
import (
"encoding/json"
"github.com/gorilla/websocket"
"log"
)
func (c *StreamClient) reader(startChan chan error) {
defer func() {
c.Conn.Close()
}()
for {
_, msg, err := c.Conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Println("error: %v", err)
}
break
}
//fmt.Println(string(msg))
var message struct {
C string `json:"C,omitempty"` // present for persistent connection messages
I string `json:"I,omitempty"` // present for method result messages
}
err = json.Unmarshal(msg, &message)
if err != nil {
c.OnError(err)
}
if message.C != "" {
var pcm PersistentConnectionMessage
err = json.Unmarshal(msg, &pcm)
if err != nil {
c.OnError(err)
}
if pcm.S == 1 {
startChan <- c.sendStart()
} else {
//assume any message that makes it this far is an array of client method calls
var calls []ClientMethodCall
err = json.Unmarshal(pcm.M, &calls)
if err != nil {
c.OnError(err)
}
c.clientMethodHandler(calls)
}
} else if message.I != "" {
c.methodResultHandler(msg)
}
}
}
type PersistentConnectionMessage struct {
C string //message id, present for all non-KeepAlive messages
M json.RawMessage //an array containing actual data
S int //indicates that the transport was initialized (a.k.a. init message)
G string //groups token – an encrypted string representing group membership
}