Poloniex 新 Websocket API
Poloniex New Websocket API
我正在尝试使用 new websocket API with Poloniex。 Wamp 现在已被弃用,关于如何对实时 public 数据(如音量等)进行简单调用的信息很少
我发现这段代码似乎实现了这个套接字,但出现在 bitestream 中。当我 运行 代码调用 header 正常并得到响应 header 也正常并且似乎发送和接收数据但随后关闭套接字。怎样才能订阅音量频道并解码流媒体数据?
import websocket
import threading
import time
import json
from threading import Thread
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ON OPEN")
def run(*args):
ws.send(json.dumps({'command':'subscribe','channel':1003}))
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
threading.Thread(target=run).start()
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
Am expecting a streaming connection to volume data but get this:
ON OPEN
send: b"\x81\xa9.)\x1d\xb8U\x0b~\xd7CD|\xd6J\x0b'\x98\x0cZh\xda]Jo\xd1LL?\x94\x0e\x0b~\xd0OGs\xddB\x0b'\x98\x1f\x19-\x8bS"
[1003,1]
unorderable types: float() > NoneType()
send: b'\x88\x82fWPGe\xbf'
### closed ###
Polo 似乎认为我已订阅:
奇怪,因为我无法重现你的错误,试试这个:
import websocket
import threading
import time
import json
from threading import Thread
def on_message(ws, message):
json_msg = json.loads(message)
channel_id = int(json_msg[0])
if (channel_id==1003) and len(json_msg)>2:
print("volume update")
print(json_msg[2][2])
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ON OPEN")
def run(*args):
ws.send(json.dumps({'command':'subscribe','channel':"1003"}))
threading.Thread(target=run).start()
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
哪个给你:
[...]
ON OPEN
send: '\x81\xab\xc0\xe6~#\xbb\xc4\x1dL\xad\x8b\x1fM\xa4\xc4D\x03\xe2\x95\x0bA\xb3\x85\x0cJ\xa2\x83\\x0f\xe0\xc4\x1dK\xa1\x88\x10F\xac\xc4D\x03\xe2\xd7N\x13\xf3\xc4\x03'
volume update
{u'USDT': u'5340084.934', u'ETH': u'914.931', u'XMR': u'194.895', u'BTC': u'1927.227', u'USDC': u'1275367.951'}
我正在尝试使用 new websocket API with Poloniex。 Wamp 现在已被弃用,关于如何对实时 public 数据(如音量等)进行简单调用的信息很少
我发现这段代码似乎实现了这个套接字,但出现在 bitestream 中。当我 运行 代码调用 header 正常并得到响应 header 也正常并且似乎发送和接收数据但随后关闭套接字。怎样才能订阅音量频道并解码流媒体数据?
import websocket
import threading
import time
import json
from threading import Thread
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ON OPEN")
def run(*args):
ws.send(json.dumps({'command':'subscribe','channel':1003}))
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
threading.Thread(target=run).start()
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
Am expecting a streaming connection to volume data but get this:
ON OPEN
send: b"\x81\xa9.)\x1d\xb8U\x0b~\xd7CD|\xd6J\x0b'\x98\x0cZh\xda]Jo\xd1LL?\x94\x0e\x0b~\xd0OGs\xddB\x0b'\x98\x1f\x19-\x8bS"
[1003,1]
unorderable types: float() > NoneType()
send: b'\x88\x82fWPGe\xbf'
### closed ###
Polo 似乎认为我已订阅:
奇怪,因为我无法重现你的错误,试试这个:
import websocket
import threading
import time
import json
from threading import Thread
def on_message(ws, message):
json_msg = json.loads(message)
channel_id = int(json_msg[0])
if (channel_id==1003) and len(json_msg)>2:
print("volume update")
print(json_msg[2][2])
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("ON OPEN")
def run(*args):
ws.send(json.dumps({'command':'subscribe','channel':"1003"}))
threading.Thread(target=run).start()
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
哪个给你:
[...]
ON OPEN
send: '\x81\xab\xc0\xe6~#\xbb\xc4\x1dL\xad\x8b\x1fM\xa4\xc4D\x03\xe2\x95\x0bA\xb3\x85\x0cJ\xa2\x83\\x0f\xe0\xc4\x1dK\xa1\x88\x10F\xac\xc4D\x03\xe2\xd7N\x13\xf3\xc4\x03'
volume update
{u'USDT': u'5340084.934', u'ETH': u'914.931', u'XMR': u'194.895', u'BTC': u'1927.227', u'USDC': u'1275367.951'}