forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lakebtcwebsocket.go
120 lines (100 loc) · 2.65 KB
/
lakebtcwebsocket.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
package main
import (
"fmt"
"github.com/gorilla/websocket"
"log"
"net/http"
)
const (
LAKEBTC_WEBSOCKET_URL = "wss://www.LakeBTC.com/websocket"
)
func WSRailsSubscribe(channel string, conn *websocket.Conn) {
data := fmt.Sprintf(`["websocket_rails.subscribe", {"data":{"channel": "%s" }}]`, channel)
err := conn.WriteMessage(websocket.TextMessage, []byte(data))
if err != nil {
log.Println(err)
return
}
}
func WSRailsUnsubscribe(channel string, conn *websocket.Conn) {
data := fmt.Sprintf(`["websocket_rails.unsubscribe", {"data":{"channel": "%s" }}]`, channel)
err := conn.WriteMessage(websocket.TextMessage, []byte(data))
if err != nil {
log.Println(err)
return
}
}
func WSRailsPong(id string, conn *websocket.Conn) {
data := fmt.Sprintf(`["websocket_rails.pong", {"data":{"connection_id": %s}}]`, id)
err := conn.WriteMessage(websocket.TextMessage, []byte(data))
if err != nil {
log.Println(err)
return
}
}
func (l *LakeBTC) WebsocketClient() {
for l.Enabled && l.Websocket {
var Dialer websocket.Dialer
conn, _, err := Dialer.Dial(LAKEBTC_WEBSOCKET_URL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", l.GetName(), err)
continue
}
log.Printf("%s Connected to Websocket.\n", l.GetName())
for l.Enabled && l.Websocket {
msgType, resp, err := conn.ReadMessage()
if err != nil {
log.Println(err)
break
}
response := [][]interface{}{}
err = JSONDecode(resp, &response)
if err != nil {
log.Println(err)
break
}
if msgType == websocket.TextMessage {
event := response[0][0]
data := response[0][1]
switch event {
case "client_connected":
WSRailsSubscribe("ticker", conn)
for _, x := range l.EnabledPairs {
currency := x[3:]
WSRailsSubscribe(fmt.Sprintf("orderbook_%s", currency), conn)
}
case "websocket_rails.subscribe":
case "websocket_rails.ping":
WSRailsPong("null", conn)
case "update":
update := data.(map[string]interface{})
channel := update["channel"]
data = update["data"]
dataJSON, err := JSONEncode(data)
if err != nil {
log.Println(err)
continue
}
switch channel {
case "ticker":
ticker := LakeBTCTickerResponse{}
err = JSONDecode(dataJSON, &ticker)
if err != nil {
log.Println(err)
continue
}
case "orderbook_USD", "orderbook_CNY":
orderbook := LakeBTCOrderbook{}
err = JSONDecode(dataJSON, &orderbook)
if err != nil {
log.Println(err)
continue
}
}
}
}
}
conn.Close()
log.Printf("%s Websocket client disconnected.\n", l.GetName())
}
}