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 ####################### ## 现货相关