package tick import "golang.org/x/net/websocket" import "fmt" import "log" import "time" import "tickserver/markinfo" type OKClient struct { origin string url string ws *websocket.Conn } func NewOKClient(orgin string, url string) *OKClient { okclient := &OKClient{} okclient.origin = orgin okclient.url = url return okclient } func (this *OKClient) Connect() (*websocket.Conn, error) { var ws *websocket.Conn var err error for i := 0; i < 10; i++ { ws, err = websocket.Dial(this.url, "", this.origin) if err != nil { fmt.Println(err) time.Sleep(time.Second * 2) } else { break } } if err != nil { log.Println("Connect", err) return ws, err } fmt.Println("ok") this.ws = ws return ws, err } func (this *OKClient) Send(strMessage string) error { if this.ws != nil { //websocket.Message.Receive(this.ws, v) if _, err := this.ws.Write([]byte(strMessage)); err != nil { log.Println("Send", strMessage, err) return err } } return nil } /* func (this *OKClient) JsonSend(strMessage string) error { if this.ws != nil { err := websocket.JSON.Send(this.ws, &msg) if _, err := this.ws.Write([]byte(strMessage)); err != nil { log.Fatal(err) return err } } return nil } */ func (this *OKClient) Close() error { if this.ws != nil { this.ws.Close() this.ws = nil } return nil } func (this *OKClient) Rev(bMessage []byte) (int, error) { if this.ws != nil { n := -1 var err error if n, err = this.ws.Read(bMessage); err != nil { log.Fatal("Rev", err) return -1, err } return n, nil } return -1, nil } func (this *OKClient) RevString(strMessage *string) error { if this.ws != nil { var err error if err = websocket.Message.Receive(this.ws, strMessage); err != nil { return err } return nil } return nil } const ( BTC_REAL = iota BTC_HIS BTC_F_REAL BTC_F_HIS ) type OKClientWrapper struct { okclient *OKClient datatyp int datastr string lastsec int64 symbol int } func NewOKClientWrapper(orgin string, url string, datatyp, symbol int) (*OKClientWrapper, error) { okclient := NewOKClient(orgin, url) okclient.Connect() okwrapper := &OKClientWrapper{} okwrapper.okclient = okclient okwrapper.datatyp = datatyp okwrapper.symbol = symbol go okwrapper.DoHeartbeat() return okwrapper, nil } func (this *OKClientWrapper) Emit(channel string) error { cmd := "{'event':'addChannel','channel':'" cmd += channel cmd += "'}" return this.okclient.Send(cmd) } func (this *OKClientWrapper) Emitex(channel string, parameter string) error { cmd := "{'event':'addChannel','channel':'" cmd += channel cmd += "','parameters':{" //{ cmd += parameter cmd += "}}" //} // cmd := "{'event':'addChannel','channel':'ok_spotcny_trade','parameters':{'api_key':'3a49ab2a-01e0-4f4d-93a5-f3399edcf84a','sign':'B5DA5241F526E1159D13C8EBE6893FEB','symbol':'btc_cny','type':'sell','price':'2180','amount':'0.001'}}" fmt.Println("sdssss", cmd) return this.okclient.Send(cmd) } func (this *OKClientWrapper) Remove(channel string) error { cmd := "{'event':'removeChannel','channel':'" cmd += channel cmd += "'}" return this.okclient.Send(cmd) } func (this *OKClientWrapper) Ping() error { cmd := "{'event':'ping'}" return this.okclient.Send(cmd) } func (this *OKClientWrapper) DoHeartbeat() { for { this.Ping() time.Sleep(time.Second * 5) if time.Now().Unix()-this.lastsec > 15 { log.Println(this.symbol, "time out!") this.okclient.Close() time.Sleep(time.Second * 2) this.okclient.Connect() time.Sleep(time.Second * 3) this.ReEmit() } } } func (this *OKClientWrapper) ReEmit() { switch this.datatyp { case BTC_REAL: if this.symbol == markinfo.BTCCNY { this.Emit("ok_btccny_ticker") } else { this.Emit("ok_btcusd_ticker") } case BTC_HIS: if this.symbol == markinfo.BTCCNY { this.Emit("ok_btccny_kline_1min") } else { this.Emit("ok_btcusd_kline_1min") } case BTC_F_REAL: this.Emit("ok_btcusd_future_ticker_this_week") case BTC_F_HIS: this.Emit("ok_btcusd_kline_this_week_1min") } }