forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinbasepro_websocket.go
127 lines (111 loc) · 2.71 KB
/
coinbasepro_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
package coinbasepro
import (
"log"
"net/http"
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
)
const (
coinbaseproWebsocketURL = "wss://ws-feed.pro.coinbase.com"
)
// WebsocketSubscribe subscribes to a websocket connection
func (c *CoinbasePro) WebsocketSubscribe(product string, conn *websocket.Conn) error {
subscribe := WebsocketSubscribe{"subscribe", product}
json, err := common.JSONEncode(subscribe)
if err != nil {
return err
}
err = conn.WriteMessage(websocket.TextMessage, json)
if err != nil {
return err
}
return nil
}
// WebsocketClient initiates a websocket client
func (c *CoinbasePro) WebsocketClient() {
for c.Enabled && c.Websocket {
var Dialer websocket.Dialer
conn, _, err := Dialer.Dial(coinbaseproWebsocketURL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", c.GetName(), err)
continue
}
log.Printf("%s Connected to Websocket.\n", c.GetName())
currencies := []string{}
for _, x := range c.EnabledPairs {
currency := x[0:3] + "-" + x[3:]
currencies = append(currencies, currency)
}
for _, x := range currencies {
err = c.WebsocketSubscribe(x, conn)
if err != nil {
log.Printf("%s Websocket subscription error: %s\n", c.GetName(), err)
continue
}
}
if c.Verbose {
log.Printf("%s Subscribed to product messages.", c.GetName())
}
for c.Enabled && c.Websocket {
msgType, resp, err := conn.ReadMessage()
if err != nil {
log.Println(err)
break
}
switch msgType {
case websocket.TextMessage:
type MsgType struct {
Type string `json:"type"`
}
msgType := MsgType{}
err := common.JSONDecode(resp, &msgType)
if err != nil {
log.Println(err)
continue
}
switch msgType.Type {
case "error":
log.Println(string(resp))
break
case "received":
received := WebsocketReceived{}
err := common.JSONDecode(resp, &received)
if err != nil {
log.Println(err)
continue
}
case "open":
open := WebsocketOpen{}
err := common.JSONDecode(resp, &open)
if err != nil {
log.Println(err)
continue
}
case "done":
done := WebsocketDone{}
err := common.JSONDecode(resp, &done)
if err != nil {
log.Println(err)
continue
}
case "match":
match := WebsocketMatch{}
err := common.JSONDecode(resp, &match)
if err != nil {
log.Println(err)
continue
}
case "change":
change := WebsocketChange{}
err := common.JSONDecode(resp, &change)
if err != nil {
log.Println(err)
continue
}
}
}
}
conn.Close()
log.Printf("%s Websocket client disconnected.", c.GetName())
}
}