diff --git a/vnpy/api/websocket/WebsocketClient.py b/vnpy/api/websocket/WebsocketClient.py index a8b9d436..909e2ab2 100644 --- a/vnpy/api/websocket/WebsocketClient.py +++ b/vnpy/api/websocket/WebsocketClient.py @@ -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): diff --git a/vnpy/trader/gateway/okexFuturesGateway/OkexFuturesBase.py b/vnpy/trader/gateway/okexFuturesGateway/OkexFuturesBase.py index 9192cfd0..1e586f0f 100644 --- a/vnpy/trader/gateway/okexFuturesGateway/OkexFuturesBase.py +++ b/vnpy/trader/gateway/okexFuturesGateway/OkexFuturesBase.py @@ -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__() diff --git a/vnpy/trader/gateway/okexFuturesGateway/okexFuturesGateway.py b/vnpy/trader/gateway/okexFuturesGateway/okexFuturesGateway.py index 1b6f14b5..8c621578 100644 --- a/vnpy/trader/gateway/okexFuturesGateway/okexFuturesGateway.py +++ b/vnpy/trader/gateway/okexFuturesGateway/okexFuturesGateway.py @@ -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):