forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwvxwebsocket.go
69 lines (58 loc) · 1.35 KB
/
dwvxwebsocket.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
package main
import (
"github.com/gorilla/websocket"
"log"
"net/http"
)
const (
DWVX_WEBSOCKET_URL = "wss://api.dwvx.com.au:8401/v1/GetTicker/"
)
func (d *DWVX) WebsocketClient() {
for d.Enabled && d.Websocket {
var Dialer websocket.Dialer
var err error
d.WebsocketConn, _, err = Dialer.Dial(DWVX_WEBSOCKET_URL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", d.Name, err)
continue
}
if d.Verbose {
log.Printf("%s Connected to Websocket.\n", d.Name)
}
err = d.WebsocketConn.WriteMessage(websocket.TextMessage, []byte(`{"messageType": "logon"}`))
if err != nil {
log.Println(err)
return
}
for d.Enabled && d.Websocket {
msgType, resp, err := d.WebsocketConn.ReadMessage()
if err != nil {
log.Println(err)
break
}
switch msgType {
case websocket.TextMessage:
type MsgType struct {
MessageType string `json:"messageType"`
}
msgType := MsgType{}
err := JSONDecode(resp, &msgType)
if err != nil {
log.Println(err)
continue
}
switch msgType.MessageType {
case "Ticker":
ticker := AlphapointWebsocketTicker{}
err = JSONDecode(resp, &ticker)
if err != nil {
log.Println(err)
continue
}
}
}
}
d.WebsocketConn.Close()
log.Printf("%s Websocket client disconnected.", d.Name)
}
}