修改okcoinGateway的断线重连功能
This commit is contained in:
parent
15a51865d6
commit
1ae6e03dfe
@ -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'服务器连接成功')
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user