From 5e6f81e544aee1cafc395ea0cd8c7d69205d9e2e Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 31 Oct 2018 05:07:44 -0400 Subject: [PATCH] =?UTF-8?q?[Fix]=20=E4=BF=AE=E5=A4=8D=E5=AF=B9OkexWebSocke?= =?UTF-8?q?t=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=9AOkexWebSocket=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E5=90=AF=E7=94=A8=E4=BA=86zlib=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] 增加了WebSocketClient.unpackData,用于自定义解包方法。默认解包方式为json.loads --- vnpy/api/websocket/WebsocketClient.py | 13 ++++++++++++- .../gateway/okexFuturesGateway/OkexFuturesBase.py | 2 +- .../okexFuturesGateway/okexFuturesGateway.py | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) 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):