From cf6b0051a00a8a2cde1c41ca0c981443e1e97130 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 30 Sep 2015 16:56:51 +1000 Subject: [PATCH] Modified Websocket ticker handling to reflect Exchange JSON changes. --- okcoinwebsocket.go | 62 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/okcoinwebsocket.go b/okcoinwebsocket.go index 2c2aad1f..e2cb300b 100644 --- a/okcoinwebsocket.go +++ b/okcoinwebsocket.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "net/url" + "reflect" "strconv" "strings" "time" @@ -35,13 +36,13 @@ type OKCoinWebsocketFutureIndex struct { } type OKCoinWebsocketTicker struct { - Timestamp int64 `json:"timestamp,string"` - Vol string `json:"vol"` - Buy float64 `json:"buy,string"` - High float64 `json:"high,string"` - Last float64 `json:"last,string"` - Low float64 `json:"low,string"` - Sell float64 `json:"sell,string"` + Timestamp float64 + Vol string + Buy float64 + High float64 + Last float64 + Low float64 + Sell float64 } type OKCoinWebsocketFuturesTicker struct { @@ -498,12 +499,51 @@ func (o *OKCoin) WebsocketClient() { switch true { case StringContains(channelStr, "ticker") && !StringContains(channelStr, "future"): + tickerValues := []string{"buy", "high", "last", "low", "sell", "timestamp"} + tickerMap := data.(map[string]interface{}) ticker := OKCoinWebsocketTicker{} - err = JSONDecode(dataJSON, &ticker) + ticker.Vol = tickerMap["vol"].(string) - if err != nil { - log.Println(err) - continue + for _, z := range tickerValues { + result := reflect.TypeOf(tickerMap[z]).String() + if result == "string" { + value, err := strconv.ParseFloat(tickerMap[z].(string), 64) + if err != nil { + log.Println(err) + continue + } + + switch z { + case "buy": + ticker.Buy = value + case "high": + ticker.High = value + case "last": + ticker.Last = value + case "low": + ticker.Low = value + case "sell": + ticker.Sell = value + case "timestamp": + ticker.Timestamp = value + } + + } else if result == "float64" { + switch z { + case "buy": + ticker.Buy = tickerMap[z].(float64) + case "high": + ticker.High = tickerMap[z].(float64) + case "last": + ticker.Last = tickerMap[z].(float64) + case "low": + ticker.Low = tickerMap[z].(float64) + case "sell": + ticker.Sell = tickerMap[z].(float64) + case "timestamp": + ticker.Timestamp = tickerMap[z].(float64) + } + } } case StringContains(channelStr, "ticker") && StringContains(channelStr, "future"): ticker := OKCoinWebsocketFuturesTicker{}