Files
gocryptotrader/exchanges/alphapoint/alphapoint_websocket.go
Ryan O'Hara-Reid 0c5d75b22c (Engine) Variety of engine updates (#390)
* drop common uuid v4 func and imported package as needed

* removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests.

* add in string

* explicitly throw away return error value

* atleast return the error that websocket initialise returns

* return error when not connected

* fix comment

* Adds comments

* move package declarations

* drop append whenever we call supported

* remove unused import

* Change incorrect spelling

* fix tests

* fix go import issue
2019-12-03 10:06:08 +11:00

72 lines
1.6 KiB
Go

package alphapoint
import (
"encoding/json"
"net/http"
"github.com/gorilla/websocket"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
const (
alphapointDefaultWebsocketURL = "wss://sim3.alphapoint.com:8401/v1/GetTicker/"
)
// WebsocketClient starts a new webstocket connection
func (a *Alphapoint) WebsocketClient() {
for a.Enabled {
var dialer websocket.Dialer
var err error
a.WebsocketConn, _, err = dialer.Dial(a.API.Endpoints.WebsocketURL, http.Header{})
if err != nil {
log.Errorf(log.ExchangeSys, "%s Unable to connect to Websocket. Error: %s\n", a.Name, err)
continue
}
if a.Verbose {
log.Debugf(log.ExchangeSys, "%s Connected to Websocket.\n", a.Name)
}
err = a.WebsocketConn.WriteMessage(websocket.TextMessage, []byte(`{"messageType": "logon"}`))
if err != nil {
log.Error(log.ExchangeSys, err)
return
}
for a.Enabled {
msgType, resp, err := a.WebsocketConn.ReadMessage()
if err != nil {
a.Websocket.ReadMessageErrors <- err
log.Error(log.ExchangeSys, err)
break
}
if msgType == websocket.TextMessage {
type MsgType struct {
MessageType string `json:"messageType"`
}
msgType := MsgType{}
err := json.Unmarshal(resp, &msgType)
if err != nil {
log.Error(log.ExchangeSys, err)
continue
}
if msgType.MessageType == "Ticker" {
ticker := WebsocketTicker{}
err = json.Unmarshal(resp, &ticker)
if err != nil {
log.Error(log.ExchangeSys, err)
continue
}
}
}
}
a.WebsocketConn.Close()
log.Debugf(log.ExchangeSys, "%s Websocket client disconnected.", a.Name)
}
}