mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Added Websocket support for OKCoin exchanges (International and Chinese)
This commit is contained in:
@@ -142,7 +142,7 @@
|
||||
"BaseCurrencies": "CNY",
|
||||
"Enabled": true,
|
||||
"Verbose": false,
|
||||
"Websocket": false,
|
||||
"Websocket": true,
|
||||
"PollingDelay": 10
|
||||
},
|
||||
{
|
||||
|
||||
@@ -13,6 +13,8 @@ const (
|
||||
OKCOIN_API_URL = "https://www.okcoin.com/api/v1/"
|
||||
OKCOIN_API_URL_CHINA = "https://www.okcoin.cn/api/v1/"
|
||||
OKCOIN_API_VERSION = "1"
|
||||
OKCOIN_WEBSOCKET_URL = "wss://real.okcoin.com:10440/websocket/okcoinapi"
|
||||
OKCOIN_WEBSOCKET_URL_CHINA = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
|
||||
)
|
||||
|
||||
type OKCoin struct {
|
||||
@@ -20,6 +22,7 @@ type OKCoin struct {
|
||||
Enabled bool
|
||||
Verbose bool
|
||||
Websocket bool
|
||||
WebsocketURL string
|
||||
PollingDelay time.Duration
|
||||
APIUrl, PartnerID, SecretKey string
|
||||
TakerFee, MakerFee float64
|
||||
@@ -57,8 +60,10 @@ type OKCoinFuturesTickerResponse struct {
|
||||
func (o *OKCoin) SetDefaults() {
|
||||
if (o.APIUrl == OKCOIN_API_URL) {
|
||||
o.Name = "OKCOIN International"
|
||||
o.WebsocketURL = OKCOIN_WEBSOCKET_URL
|
||||
} else if (o.APIUrl == OKCOIN_API_URL_CHINA) {
|
||||
o.Name = "OKCOIN China"
|
||||
o.WebsocketURL = OKCOIN_WEBSOCKET_URL_CHINA
|
||||
}
|
||||
o.Enabled = true
|
||||
o.Verbose = false
|
||||
@@ -101,9 +106,18 @@ func (o *OKCoin) GetFee(maker bool) (float64) {
|
||||
|
||||
func (o *OKCoin) Run() {
|
||||
if o.Verbose {
|
||||
log.Printf("%s Websocket: %s (url: %s).", o.GetName(), IsEnabled(o.Websocket), o.WebsocketURL)
|
||||
log.Printf("%s polling delay: %ds.\n", o.GetName(), o.PollingDelay)
|
||||
}
|
||||
|
||||
if o.Websocket {
|
||||
if o.WebsocketURL == OKCOIN_WEBSOCKET_URL {
|
||||
go o.WebsocketClient([]string{"btcusd", "ltcusd"})
|
||||
} else {
|
||||
go o.WebsocketClient([]string{"btccny", "ltccny"})
|
||||
}
|
||||
}
|
||||
|
||||
for o.Enabled {
|
||||
if o.APIUrl == OKCOIN_API_URL {
|
||||
go func() {
|
||||
|
||||
104
okcoinwebsocket.go
Normal file
104
okcoinwebsocket.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type OKCoinWebsocketEvent struct {
|
||||
Event string `json:"event"`
|
||||
Channel string `json:"channel"`
|
||||
}
|
||||
|
||||
type OKCoinWebsocketResponse struct {
|
||||
Channel string `json:"channel"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
var okConn websocket.Conn
|
||||
|
||||
func (o *OKCoin) PingHandler(message string) (error) {
|
||||
err := okConn.WriteControl(websocket.PingMessage, []byte("{'event':'ping'}"), time.Now().Add(time.Second))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *OKCoin) AddChannel(conn *websocket.Conn, channel string) {
|
||||
event := OKCoinWebsocketEvent{"addChannel", channel}
|
||||
json, err := JSONEncode(event)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
err = conn.WriteMessage(websocket.TextMessage, json)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (o* OKCoin) RemoveChannel(conn *websocket.Conn, channel string) {
|
||||
event := OKCoinWebsocketEvent{"removeChannel", channel}
|
||||
json, err := JSONEncode(event)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
err = conn.WriteMessage(websocket.TextMessage, json)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (o *OKCoin) WebsocketClient(currencies []string) {
|
||||
if len(currencies) == 0 {
|
||||
log.Println("No currencies for Websocket client specified.")
|
||||
return
|
||||
}
|
||||
|
||||
var Dialer websocket.Dialer
|
||||
okConn, resp, err := Dialer.Dial(o.WebsocketURL, http.Header{})
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if o.Verbose {
|
||||
log.Printf("%s Connected to Websocket.", o.GetName())
|
||||
log.Println(resp)
|
||||
}
|
||||
|
||||
okConn.SetPingHandler(o.PingHandler)
|
||||
|
||||
for _, x := range currencies {
|
||||
o.AddChannel(okConn, fmt.Sprintf("ok_%s_ticker", x))
|
||||
o.AddChannel(okConn, fmt.Sprintf("ok_%s_depth", x))
|
||||
o.AddChannel(okConn, fmt.Sprintf("ok_%s_trades", x))
|
||||
}
|
||||
|
||||
for {
|
||||
msgType, resp, err := okConn.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
break
|
||||
}
|
||||
switch msgType {
|
||||
case websocket.TextMessage:
|
||||
log.Println("\n" + string(resp))
|
||||
}
|
||||
}
|
||||
okConn.Close()
|
||||
log.Printf("%s Websocket client disconnected.", o.GetName())
|
||||
}
|
||||
Reference in New Issue
Block a user