Merge pull request #1205 from nanoric/okex_compressed_websocket

[Fix] Okex compressed websocket
This commit is contained in:
vn.py 2018-11-01 10:08:20 +08:00 committed by GitHub
commit 6cf6bfb9b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -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):

View File

@ -121,7 +121,7 @@ class OkexFuturesWebSocketBase(WebsocketClient):
Okex期货websocket客户端
实例化后使用init设置apiKey和secretKeyapiSecret
"""
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__()

View File

@ -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):