hadrianl / huobi

火币的行情交易的python实现
MIT License
154 stars 64 forks source link

websocket not working with the compressed data #4

Closed ghost closed 5 years ago

ghost commented 6 years ago

Exception has occurred: websocket._exceptions.WebSocketConnectionClosedException socket is already closed. File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/websocket/_socket.py", line 114, in send raise WebSocketConnectionClosedException("socket is already closed.") File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/websocket/_core.py", line 437, in _send return send(self.sock, data) File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/websocket/_core.py", line 268, in send_frame data = data[l:] File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/websocket/_core.py", line 242, in send return self.send_frame(frame) File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/websocket/_app.py", line 153, in send if not self.sock or self.sock.send(data, opcode) == 0: File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/huobitrade/service.py", line 47, in send_message self.ws.send(msg_json) File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/env/lib/python3.7/site-packages/huobitrade/service.py", line 213, in sub_kline self.send_message(msg) File "/Users/xxx/Projects/qf/questrade/questrade-model-decision/tmp.py", line 8, in hb.sub_kline('ethbtc', '1min') # 订阅数据 File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec)

Seems the data from huobi is compressed,

"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xabV*\xc8\xccKW\xb2245636\xb14643\xb74\xae\x05\x00m\xe4\xba]\x16\x00\x00\x00"

is

{"ping":1536349316793}

hadrianl commented 6 years ago

check it out tomorrow

hadrianl commented 6 years ago

应该不是数据解压问题,on_message函数的json_data = gz.decompress(_msg).decode()已经解压数据了的,您可以把代码发一下。这很有可能是你在websocket未连接上的时候已经发起订阅造成的,一般可以在run之后sleep一下,保证websocket跟火币服务器链接了再去发起订阅

hadrianl commented 5 years ago

如果遇到Exception has occurred: websocket._exceptions.WebSocketConnectionClosedException socket is already closed.多数是因为ws还未正式连接成功之前已经发送的订阅请求出去,解决该问题的两个方案是1.直接sleep一下 2.使用after_open或者是after_auth的装饰器来控制连接成功后的订阅请求行为。 推荐后者!详见首页readme有用法说明