使用 Python autbahn 或其他套接字模块读取 Poloniex Trollbox 上的消息?

2023-12-10

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 正在开发一个pure 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python autbahn 或其他套接字模块读取 Poloniex Trollbox 上的消息? 的相关文章

随机推荐