From 1ae6e03dfe7d07b81fda40e3343554ebc5109c28 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Wed, 28 Dec 2016 22:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9okcoinGateway=E7=9A=84?= =?UTF-8?q?=E6=96=AD=E7=BA=BF=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.trader/okcoinGateway/okcoinGateway.py | 23 ++++++++++++++++++----- vn.trader/okcoinGateway/vnokcoin.py | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/vn.trader/okcoinGateway/okcoinGateway.py b/vn.trader/okcoinGateway/okcoinGateway.py index 2a2e5a9f..917a070e 100644 --- a/vn.trader/okcoinGateway/okcoinGateway.py +++ b/vn.trader/okcoinGateway/okcoinGateway.py @@ -15,6 +15,7 @@ from datetime import datetime from copy import copy from threading import Condition from Queue import Queue +from threading import Thread import vnokcoin from vtGateway import * @@ -242,7 +243,7 @@ class Api(vnokcoin.OkCoinApi): self.gatewayName = gateway.gatewayName # gateway对象名称 self.active = False # 若为True则会在断线后自动重连 - + self.cbDict = {} self.tickDict = {} self.orderDict = {} @@ -274,17 +275,29 @@ class Api(vnokcoin.OkCoinApi): #---------------------------------------------------------------------- def onClose(self, ws): """接口断开""" + # 如果尚未连上,则忽略该次断开提示 + if not self.gateway.connected: + return + self.gateway.connected = False self.writeLog(u'服务器连接断开') # 重新连接 if self.active: - self.writeLog(u'等待5秒后重新连接') - sleep(5) - self.reconnect() + + def reconnect(): + while not self.gateway.connected: + self.writeLog(u'等待10秒后重新连接') + sleep(10) + if not self.gateway.connected: + self.reconnect() + + t = Thread(target=reconnect) + t.start() #---------------------------------------------------------------------- - def onOpen(self, ws): + def onOpen(self, ws): + """连接成功""" self.gateway.connected = True self.writeLog(u'服务器连接成功') diff --git a/vn.trader/okcoinGateway/vnokcoin.py b/vn.trader/okcoinGateway/vnokcoin.py index d27d7110..38565825 100644 --- a/vn.trader/okcoinGateway/vnokcoin.py +++ b/vn.trader/okcoinGateway/vnokcoin.py @@ -185,7 +185,7 @@ class OkCoinApi(object): #---------------------------------------------------------------------- def close(self): """关闭接口""" - if self.thread and self.thread.isAlive: + if self.thread and self.thread.isAlive(): self.ws.close() self.thread.join()