From 5eaefd05374f660a6905b3d0d2b3fbe4a864cae4 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Mon, 21 Nov 2016 21:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0okcoin=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E9=87=8D=E8=BF=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.okcoin/vnokcoin.py | 30 ++++++++++++++++++++++-- vn.trader/okcoinGateway/okcoinGateway.py | 23 ++++++++++++------ vn.trader/okcoinGateway/vnokcoin.py | 30 ++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/vn.okcoin/vnokcoin.py b/vn.okcoin/vnokcoin.py index 7aeac6f6..334d6f42 100644 --- a/vn.okcoin/vnokcoin.py +++ b/vn.okcoin/vnokcoin.py @@ -166,6 +166,22 @@ class OkCoinApi(object): self.thread = Thread(target=self.ws.run_forever) self.thread.start() + #---------------------------------------------------------------------- + def reconnect(self): + """重新连接""" + # 首先关闭之前的连接 + self.close() + + # 再执行重连任务 + self.ws = websocket.WebSocketApp(self.host, + on_message=self.onMessage, + on_error=self.onError, + on_close=self.onClose, + on_open=self.onOpen) + + self.thread = Thread(target=self.ws.run_forever) + self.thread.start() + #---------------------------------------------------------------------- def close(self): """关闭接口""" @@ -184,7 +200,12 @@ class OkCoinApi(object): # 使用json打包并发送 j = json.dumps(d) - self.ws.send(j) + + # 若触发异常则重连 + try: + self.ws.send(j) + except websocket.WebSocketConnectionClosedException: + pass #---------------------------------------------------------------------- def sendTradingRequest(self, channel, params): @@ -202,7 +223,12 @@ class OkCoinApi(object): # 使用json打包并发送 j = json.dumps(d) - self.ws.send(j) + + # 若触发异常则重连 + try: + self.ws.send(j) + except websocket.WebSocketConnectionClosedException: + pass ####################### ## 现货相关 diff --git a/vn.trader/okcoinGateway/okcoinGateway.py b/vn.trader/okcoinGateway/okcoinGateway.py index 8d524a7e..ab82da62 100644 --- a/vn.trader/okcoinGateway/okcoinGateway.py +++ b/vn.trader/okcoinGateway/okcoinGateway.py @@ -141,7 +141,8 @@ class OkcoinGateway(VtGateway): host = vnokcoin.OKCOIN_CNY else: host = vnokcoin.OKCOIN_USD - + + self.api.active = True self.api.connect(host, apiKey, secretKey, trace) log = VtLogData() @@ -181,6 +182,7 @@ class OkcoinGateway(VtGateway): #---------------------------------------------------------------------- def close(self): """关闭""" + self.api.active = False self.api.close() #---------------------------------------------------------------------- @@ -238,13 +240,12 @@ class Api(vnokcoin.OkCoinApi): self.gateway = gateway # gateway对象 self.gatewayName = gateway.gatewayName # gateway对象名称 + self.active = False # 若为True则会在断线后自动重连 + self.cbDict = {} self.tickDict = {} self.orderDict = {} - self.lastOrderID = '' - self.orderCondition = Condition() - self.localNo = 0 # 本地委托号 self.localNoQueue = Queue() # 未收到系统委托号的本地委托号队列 self.localNoDict = {} # key为本地委托号,value为系统委托号 @@ -272,9 +273,15 @@ class Api(vnokcoin.OkCoinApi): #---------------------------------------------------------------------- def onClose(self, ws): """接口断开""" - self.gateway.connected = True + self.gateway.connected = False self.writeLog(u'服务器连接断开') + # 重新连接 + if self.active: + print 'start reconnect' + self.reconnect() + print 'reconnected' + #---------------------------------------------------------------------- def onOpen(self, ws): self.gateway.connected = True @@ -380,7 +387,7 @@ class Api(vnokcoin.OkCoinApi): tick.lowPrice = float(rawData['low']) tick.lastPrice = float(rawData['last']) tick.volume = float(rawData['vol'].replace(',', '')) - tick.date, tick.time = generateDateTime(rawData['timestamp']) + #tick.date, tick.time = generateDateTime(rawData['timestamp']) newtick = copy(tick) self.gateway.onTick(newtick) @@ -417,7 +424,9 @@ class Api(vnokcoin.OkCoinApi): tick.askPrice2, tick.askVolume2 = rawData['asks'][1] tick.askPrice3, tick.askVolume3 = rawData['asks'][2] tick.askPrice4, tick.askVolume4 = rawData['asks'][3] - tick.askPrice5, tick.askVolume5 = rawData['asks'][4] + tick.askPrice5, tick.askVolume5 = rawData['asks'][4] + + tick.date, tick.time = generateDateTime(rawData['timestamp']) newtick = copy(tick) self.gateway.onTick(newtick) diff --git a/vn.trader/okcoinGateway/vnokcoin.py b/vn.trader/okcoinGateway/vnokcoin.py index 7aeac6f6..334d6f42 100644 --- a/vn.trader/okcoinGateway/vnokcoin.py +++ b/vn.trader/okcoinGateway/vnokcoin.py @@ -166,6 +166,22 @@ class OkCoinApi(object): self.thread = Thread(target=self.ws.run_forever) self.thread.start() + #---------------------------------------------------------------------- + def reconnect(self): + """重新连接""" + # 首先关闭之前的连接 + self.close() + + # 再执行重连任务 + self.ws = websocket.WebSocketApp(self.host, + on_message=self.onMessage, + on_error=self.onError, + on_close=self.onClose, + on_open=self.onOpen) + + self.thread = Thread(target=self.ws.run_forever) + self.thread.start() + #---------------------------------------------------------------------- def close(self): """关闭接口""" @@ -184,7 +200,12 @@ class OkCoinApi(object): # 使用json打包并发送 j = json.dumps(d) - self.ws.send(j) + + # 若触发异常则重连 + try: + self.ws.send(j) + except websocket.WebSocketConnectionClosedException: + pass #---------------------------------------------------------------------- def sendTradingRequest(self, channel, params): @@ -202,7 +223,12 @@ class OkCoinApi(object): # 使用json打包并发送 j = json.dumps(d) - self.ws.send(j) + + # 若触发异常则重连 + try: + self.ws.send(j) + except websocket.WebSocketConnectionClosedException: + pass ####################### ## 现货相关