使用Python autbahn或其他套接字模块在Poloniex Trollbox上阅读消息?
作者:互联网
Poloniex不会将每条消息都返回给我的套接字.我使用以下代码阅读消息,有时我会收到连续的消息号,但有时会丢失10条消息:
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine
class PoloniexComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
@coroutine
def onJoin(self, details):
def onTrollbox(*args):
print("type: ", args[0])
print("message_number: ", args[1])
print("user_name: ", args[2])
print("message: ", args[3])
print("reputation: ", args[4])
try:
yield from self.subscribe(onTrollbox, 'trollbox')
except Exception as e:
print("Could not subscribe to topic:", e)
runner = ApplicationRunner("wss://api.poloniex.com", "realm1")
runner.run(PoloniexComponent)
有人知道更好的解决方案吗?我试过这个,但它根本不起作用:
from websocket import create_connection
ws = create_connection("wss://api.poloniex.com")
ws.send("trollbox")
result = ws.recv()
print "Received '%s'" % result
ws.close()
解决方法:
这是解决方案:
WAMP API有时可能会发生这些丢失的消息.这是由于路由软件存在固有的可扩展性问题,而Poloniex正在开发一个纯WebSockets API(目前由Web界面使用,但缺少文档)来替换它.新websocket服务器的URL是wss://api2.poloniex.com:443并连接到需要发送消息的trollbox消息:'{“command”:“subscribe”,“channel”:1001}’ .
这是一个示例代码,它更容易使用:
from websocket import create_connection
import json
ws = create_connection("wss://api2.poloniex.com:443")
ws.send('{"command" : "subscribe", "channel" : 1001}')
while True:
result = ws.recv()
json_result = json.loads(result)
if len(json_result) >= 3:
print(json_result)
ws.close()
标签:python,sockets,wamp,autobahn,poloniex 来源: https://codeday.me/bug/20190823/1701214.html