forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinut_websocket.go
61 lines (50 loc) · 1.26 KB
/
coinut_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
package coinut
import (
"log"
"net/http"
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
)
const coinutWebsocketURL = "wss://wsapi.coinut.com"
// WebsocketClient initiates a websocket client
func (c *COINUT) WebsocketClient() {
for c.Enabled && c.Websocket {
var Dialer websocket.Dialer
var err error
c.WebsocketConn, _, err = Dialer.Dial(c.WebsocketURL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", c.Name, err)
continue
}
if c.Verbose {
log.Printf("%s Connected to Websocket.\n", c.Name)
}
err = c.WebsocketConn.WriteMessage(websocket.TextMessage, []byte(`{"messageType": "hello_world"}`))
if err != nil {
log.Println(err)
return
}
for c.Enabled && c.Websocket {
msgType, resp, err := c.WebsocketConn.ReadMessage()
if err != nil {
log.Println(err)
break
}
switch msgType {
case websocket.TextMessage:
type MsgType struct {
MessageType string `json:"messageType"`
}
msgType := MsgType{}
err := common.JSONDecode(resp, &msgType)
if err != nil {
log.Println(err)
continue
}
log.Println(string(resp))
}
}
c.WebsocketConn.Close()
log.Printf("%s Websocket client disconnected.", c.Name)
}
}