-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-client.go
170 lines (138 loc) · 3.67 KB
/
stream-client.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
166
167
168
169
170
package bittrex
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
"net/http"
"net/url"
"time"
)
type StreamClient struct {
ConnectionToken string
ConnectionId string
KeepAliveTimeout float64
DisconnectTimeout float64
ProtocolVersion string
TransportConnectTimeout float64
Conn *websocket.Conn
WriteChan chan []byte
Markets map[string]*Market
MethodCalls MethodCallMap
OnError func(error)
}
func NewStreamClient() *StreamClient {
client := StreamClient{
ProtocolVersion: "1.4",
OnError: func(err error) { panic(err) },
WriteChan: make(chan []byte, 20),
Markets: make(map[string]*Market),
MethodCalls: MethodCallMap{Callers: make(map[string]MethodCall)},
}
return &client
}
func (c *StreamClient) Connect() error {
err := c.negotiate()
if err != nil {
//return err
return errors.New("problem with negotiate")
}
err = c.dial()
if err != nil {
//return err
return errors.New("problem with dial")
}
startChan := make(chan error)
go c.reader(startChan)
go c.writer()
timer := time.NewTicker(10 * time.Second)
select {
case <-timer.C:
//something went wrong with the start sequence
return errors.New("timed out during start sequence")
case err = <-startChan:
timer.Stop()
close(startChan)
if err != nil {
return errors.New("problem with start")
}
return nil
}
}
func (c *StreamClient) negotiate() error {
u := url.URL{Scheme: "https", Host: "www.bittrex.com", Path: "/signalr/negotiate"}
v := url.Values{}
v.Set("clientProtocol", c.ProtocolVersion)
u.RawQuery = v.Encode()
resp, err := http.Get(u.String())
if err != nil {
return err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(c)
return err
}
func (c *StreamClient) dial() error {
u := url.URL{Scheme: "wss", Host: "www.bittrex.com", Path: "/signalr/connect"}
v := url.Values{}
v.Set("transport", "webSockets")
v.Set("clientProtocol", c.ProtocolVersion)
v.Set("connectionToken", c.ConnectionToken)
u.RawQuery = v.Encode()
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
c.Conn = conn
return nil
}
func (c *StreamClient) sendStart() error {
u := url.URL{Scheme: "https", Host: "www.bittrex.com", Path: "/signalr/start"}
v := url.Values{}
v.Set("transport", "webSockets")
v.Set("clientProtocol", c.ProtocolVersion)
v.Set("connectionToken", c.ConnectionToken)
u.RawQuery = v.Encode()
resp, err := http.Get(u.String())
if err != nil {
return err
}
defer resp.Body.Close()
var startResponse struct {
Response string
}
err = json.NewDecoder(resp.Body).Decode(&startResponse)
if err != nil {
return err
}
if startResponse.Response == "started" {
return nil
}
return errors.New("Unexpected response to start request")
}
func (c *StreamClient) Close() error {
u := url.URL{Scheme: "https", Host: "www.bittrex.com", Path: "/signalr/abort"}
v := url.Values{}
v.Set("transport", "webSockets")
v.Set("clientProtocol", c.ProtocolVersion)
v.Set("connectionToken", c.ConnectionToken)
u.RawQuery = v.Encode()
_, err := http.Get(u.String())
if err != nil {
return err
}
err = c.Conn.Close()
return err
}
type CallMethodMessage struct {
M string //name of method
I string //invocation identifier, a number to identify the response
H string `json:"H,omitempty"` //name of hub
A []string //arguments
S map[string]string //state – a dictionary containing additional custom data
}
func (c *StreamClient) writer() {
for {
msg := <-c.WriteChan
c.Conn.WriteMessage(websocket.TextMessage, msg)
}
}