-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebsocket.go
165 lines (144 loc) · 3.37 KB
/
websocket.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package idex
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/gorilla/websocket"
)
// Socket wraps the websocket connection
type Socket struct {
Conn *websocket.Conn
URL string
}
// Connect to websocket
func (s *Socket) Connect() error {
c, resp, err := websocket.DefaultDialer.Dial(s.URL, nil)
if err == websocket.ErrBadHandshake {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return fmt.Errorf("dial failed with status: %d, body: %v", resp.StatusCode, buf.String())
}
if err != nil {
return err
}
s.Conn = c
return handshake(s)
}
// Handshake with server to be done immediately after connecting
func handshake(s *Socket) error {
type Payload struct {
Type string `json:"type"`
Version string `json:"version"`
Key string `json:"key"`
}
type Handshake struct {
Method string `json:"method"`
Payload *Payload `json:"payload"`
}
hs := &Handshake{
Method: "handshake",
Payload: &Payload{
Type: "client",
Version: "2.0",
Key: "17paIsICur8sA0OBqG6dH5G1rmrHNMwt4oNk4iX9",
},
}
h, err := json.Marshal(hs)
if err != nil {
return err
}
if err := s.Conn.WriteMessage(websocket.TextMessage, h); err != nil {
return err
}
return nil
}
// Monitor the websocket for messages
func (s *Socket) Monitor(resp chan SocketResponse) {
for {
sr := SocketResponse{}
_, msg, err := s.Conn.ReadMessage()
if err != nil {
sr.Error = err
resp <- sr
continue
}
m := &Method{}
if err := json.Unmarshal(msg, m); err != nil {
sr.Error = err
resp <- sr
continue
}
switch m.Method {
case "handshake":
log.Println("Handshake successful")
case "notifyTradesInserted":
processTradesInserted(msg, resp)
case "notifyOrderInserted":
processOrderInserted(msg, resp)
case "pushCancel":
processPushCancel(msg, resp)
case "pushCancels":
processPushCancels(msg, resp)
case "pushEthPrice":
case "pushServerBlock":
case "pushRewardPoolSize":
default:
log.Printf("Other method: %+v\n", string(msg))
}
}
}
func processTradesInserted(msg []byte, c chan SocketResponse) {
p := &struct {
Payload []*TradeInserted `json:"payload"`
}{}
sr := SocketResponse{}
if err := json.Unmarshal(msg, p); err != nil {
sr.Error = fmt.Errorf("unmarshal error: %v, for message %v", err, string(msg))
c <- sr
} else {
for _, t := range p.Payload {
sr.TradeInserted = t
c <- sr
}
}
}
func processOrderInserted(msg []byte, c chan SocketResponse) {
p := &struct {
Payload *OrderInserted `json:"payload"`
}{}
sr := SocketResponse{}
if err := json.Unmarshal(msg, p); err != nil {
sr.Error = fmt.Errorf("unmarshal error: %v, for message %v", err, string(msg))
} else {
sr.OrderInserted = p.Payload
}
c <- sr
}
func processPushCancel(msg []byte, c chan SocketResponse) {
p := &struct {
Payload *PushCancel `json:"payload"`
}{}
sr := SocketResponse{}
if err := json.Unmarshal(msg, p); err != nil {
sr.Error = fmt.Errorf("unmarshal error: %v, for message %v", err, string(msg))
} else {
sr.PushCancel = p.Payload
}
c <- sr
}
func processPushCancels(msg []byte, c chan SocketResponse) {
p := &struct {
Payload []*PushCancel `json:"payload"`
}{}
sr := SocketResponse{}
if err := json.Unmarshal(msg, p); err != nil {
sr.Error = fmt.Errorf("unmarshal error: %v, for message %v", err, string(msg))
c <- sr
} else {
for _, pc := range p.Payload {
sr.PushCancel = pc
c <- sr
}
}
}