Bugfix: Websocket ping/pong improvements (#406)

* Renames func. Creates new func to setup pinghander either via gorilla style or our own

* Cleans up all ping pong handlers.......

* Clears up issues, makes naming a bit better

* Adds tests

* Adds ping support to binance

* Cleans up ping pongs and adds a comment

* Cleans up waitgroup stuff.

* DISCREETLY cleans up woeful function

* Fixes Kraken ping message type. Removes unnecessary test property. Adds `if err == websocket.ErrCloseSent {` to ping func

* +1 for +v
This commit is contained in:
Scott
2020-01-03 04:47:46 +00:00
committed by Adrian Gallagher
parent 4e05ad41e3
commit 44ac3586a0
19 changed files with 198 additions and 125 deletions

View File

@@ -96,10 +96,11 @@ func (c *Coinbene) WsDataHandler() {
return
}
c.Websocket.TrafficAlert <- struct{}{}
if string(stream.Raw) == "ping" {
c.WebsocketConn.Lock()
c.WebsocketConn.Connection.WriteMessage(websocket.TextMessage, []byte("pong"))
c.WebsocketConn.Unlock()
if string(stream.Raw) == wshandler.Ping {
err = c.WebsocketConn.SendRawMessage(websocket.TextMessage, []byte(wshandler.Pong))
if err != nil {
c.Websocket.DataHandler <- err
}
continue
}
var result map[string]interface{}
@@ -342,7 +343,7 @@ func (c *Coinbene) Subscribe(channelToSubscribe wshandler.WebsocketChannelSubscr
var sub WsSub
sub.Operation = "subscribe"
sub.Arguments = []string{channelToSubscribe.Channel}
return c.WebsocketConn.SendMessage(sub)
return c.WebsocketConn.SendJSONMessage(sub)
}
// Unsubscribe sends a websocket message to receive data from the channel
@@ -350,7 +351,7 @@ func (c *Coinbene) Unsubscribe(channelToSubscribe wshandler.WebsocketChannelSubs
var sub WsSub
sub.Operation = "unsubscribe"
sub.Arguments = []string{channelToSubscribe.Channel}
return c.WebsocketConn.SendMessage(sub)
return c.WebsocketConn.SendJSONMessage(sub)
}
// Login logs in
@@ -364,5 +365,5 @@ func (c *Coinbene) Login() error {
sign := crypto.HexEncodeToString(tempSign)
sub.Operation = "login"
sub.Arguments = []string{c.API.Credentials.Key, expTime, sign}
return c.WebsocketConn.SendMessage(sub)
return c.WebsocketConn.SendJSONMessage(sub)
}