[Fix] 修复对OkexWebSocket的支持:OkexWebSocket强制启用了zlib压缩
[Add] 增加了WebSocketClient.unpackData,用于自定义解包方法。默认解包方式为json.loads
This commit is contained in:
parent
4c39d4003e
commit
5e6f81e544
@ -155,7 +155,7 @@ class WebsocketClient(object):
|
||||
continue
|
||||
self._recordLastReceivedText(text)
|
||||
try:
|
||||
data = json.loads(text)
|
||||
data = self.unpackData(text)
|
||||
except ValueError as e:
|
||||
print('websocket unable to parse data: ' + text)
|
||||
raise e
|
||||
@ -170,6 +170,17 @@ class WebsocketClient(object):
|
||||
et, ev, tb = sys.exc_info()
|
||||
self.onError(et, ev, tb)
|
||||
self._reconnect()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
@staticmethod
|
||||
def unpackData(data):
|
||||
"""
|
||||
解密数据,默认使用json解密为dict
|
||||
解密后的数据将会传入onPacket
|
||||
如果需要使用不同的解密方式,就重载这个函数。
|
||||
:param data 收到的数据,可能是text frame,也可能是binary frame, 目前并没有区分这两者
|
||||
"""
|
||||
return json.loads(data)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def _runPing(self):
|
||||
|
@ -121,7 +121,7 @@ class OkexFuturesWebSocketBase(WebsocketClient):
|
||||
Okex期货websocket客户端
|
||||
实例化后使用init设置apiKey和secretKey(apiSecret)
|
||||
"""
|
||||
host = 'wss://real.okex.com:10440/websocket/okexapi'
|
||||
host = 'wss://real.okex.com:10440/websocket/okexapi?compress=true'
|
||||
|
||||
def __init__(self):
|
||||
super(OkexFuturesWebSocketBase, self).__init__()
|
||||
|
@ -5,6 +5,7 @@ from __future__ import print_function
|
||||
import json
|
||||
import sys
|
||||
import traceback
|
||||
import zlib
|
||||
from collections import defaultdict
|
||||
|
||||
from enum import Enum
|
||||
@ -141,6 +142,7 @@ class OkexFuturesGateway(VtGateway):
|
||||
|
||||
self.webSocket = OkexFuturesWebSocketBase()
|
||||
self.webSocket.onPacket = self.onWebSocketPacket
|
||||
self.webSocket.unpackData = self.webSocketUnpackData
|
||||
|
||||
self.leverRate = 1
|
||||
self.symbols = []
|
||||
@ -611,6 +613,12 @@ class OkexFuturesGateway(VtGateway):
|
||||
price=pos['short_avg_cost'],
|
||||
)
|
||||
self.onPosition(vtPos)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
@staticmethod
|
||||
def webSocketUnpackData(data):
|
||||
"""重载websocket.unpackData"""
|
||||
return json.loads(zlib.decompress(data, -zlib.MAX_WBITS))
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onWebSocketPacket(self, packets):
|
||||
|
Loading…
Reference in New Issue
Block a user