增加okcoin接口的重连功能

This commit is contained in:
chenxy123 2016-11-21 21:22:18 +08:00
parent 4d80596462
commit 5eaefd0537
3 changed files with 72 additions and 11 deletions

View File

@ -166,6 +166,22 @@ class OkCoinApi(object):
self.thread = Thread(target=self.ws.run_forever) self.thread = Thread(target=self.ws.run_forever)
self.thread.start() 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): def close(self):
"""关闭接口""" """关闭接口"""
@ -184,7 +200,12 @@ class OkCoinApi(object):
# 使用json打包并发送 # 使用json打包并发送
j = json.dumps(d) j = json.dumps(d)
self.ws.send(j)
# 若触发异常则重连
try:
self.ws.send(j)
except websocket.WebSocketConnectionClosedException:
pass
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendTradingRequest(self, channel, params): def sendTradingRequest(self, channel, params):
@ -202,7 +223,12 @@ class OkCoinApi(object):
# 使用json打包并发送 # 使用json打包并发送
j = json.dumps(d) j = json.dumps(d)
self.ws.send(j)
# 若触发异常则重连
try:
self.ws.send(j)
except websocket.WebSocketConnectionClosedException:
pass
####################### #######################
## 现货相关 ## 现货相关

View File

@ -142,6 +142,7 @@ class OkcoinGateway(VtGateway):
else: else:
host = vnokcoin.OKCOIN_USD host = vnokcoin.OKCOIN_USD
self.api.active = True
self.api.connect(host, apiKey, secretKey, trace) self.api.connect(host, apiKey, secretKey, trace)
log = VtLogData() log = VtLogData()
@ -181,6 +182,7 @@ class OkcoinGateway(VtGateway):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def close(self): def close(self):
"""关闭""" """关闭"""
self.api.active = False
self.api.close() self.api.close()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@ -238,13 +240,12 @@ class Api(vnokcoin.OkCoinApi):
self.gateway = gateway # gateway对象 self.gateway = gateway # gateway对象
self.gatewayName = gateway.gatewayName # gateway对象名称 self.gatewayName = gateway.gatewayName # gateway对象名称
self.active = False # 若为True则会在断线后自动重连
self.cbDict = {} self.cbDict = {}
self.tickDict = {} self.tickDict = {}
self.orderDict = {} self.orderDict = {}
self.lastOrderID = ''
self.orderCondition = Condition()
self.localNo = 0 # 本地委托号 self.localNo = 0 # 本地委托号
self.localNoQueue = Queue() # 未收到系统委托号的本地委托号队列 self.localNoQueue = Queue() # 未收到系统委托号的本地委托号队列
self.localNoDict = {} # key为本地委托号value为系统委托号 self.localNoDict = {} # key为本地委托号value为系统委托号
@ -272,9 +273,15 @@ class Api(vnokcoin.OkCoinApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onClose(self, ws): def onClose(self, ws):
"""接口断开""" """接口断开"""
self.gateway.connected = True self.gateway.connected = False
self.writeLog(u'服务器连接断开') self.writeLog(u'服务器连接断开')
# 重新连接
if self.active:
print 'start reconnect'
self.reconnect()
print 'reconnected'
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onOpen(self, ws): def onOpen(self, ws):
self.gateway.connected = True self.gateway.connected = True
@ -380,7 +387,7 @@ class Api(vnokcoin.OkCoinApi):
tick.lowPrice = float(rawData['low']) tick.lowPrice = float(rawData['low'])
tick.lastPrice = float(rawData['last']) tick.lastPrice = float(rawData['last'])
tick.volume = float(rawData['vol'].replace(',', '')) tick.volume = float(rawData['vol'].replace(',', ''))
tick.date, tick.time = generateDateTime(rawData['timestamp']) #tick.date, tick.time = generateDateTime(rawData['timestamp'])
newtick = copy(tick) newtick = copy(tick)
self.gateway.onTick(newtick) self.gateway.onTick(newtick)
@ -419,6 +426,8 @@ class Api(vnokcoin.OkCoinApi):
tick.askPrice4, tick.askVolume4 = rawData['asks'][3] 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) newtick = copy(tick)
self.gateway.onTick(newtick) self.gateway.onTick(newtick)

View File

@ -166,6 +166,22 @@ class OkCoinApi(object):
self.thread = Thread(target=self.ws.run_forever) self.thread = Thread(target=self.ws.run_forever)
self.thread.start() 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): def close(self):
"""关闭接口""" """关闭接口"""
@ -184,7 +200,12 @@ class OkCoinApi(object):
# 使用json打包并发送 # 使用json打包并发送
j = json.dumps(d) j = json.dumps(d)
self.ws.send(j)
# 若触发异常则重连
try:
self.ws.send(j)
except websocket.WebSocketConnectionClosedException:
pass
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendTradingRequest(self, channel, params): def sendTradingRequest(self, channel, params):
@ -202,7 +223,12 @@ class OkCoinApi(object):
# 使用json打包并发送 # 使用json打包并发送
j = json.dumps(d) j = json.dumps(d)
self.ws.send(j)
# 若触发异常则重连
try:
self.ws.send(j)
except websocket.WebSocketConnectionClosedException:
pass
####################### #######################
## 现货相关 ## 现货相关