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

sai*_*tam 3 python sockets wamp autobahn poloniex

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)
Run Code Online (Sandbox Code Playgroud)

有人知道更好的解决方案吗?我试过这个,但它根本不起作用:

from websocket import create_connection
ws = create_connection("wss://api.poloniex.com")
ws.send("trollbox")
result = ws.recv()
print "Received '%s'" % result
ws.close()
Run Code Online (Sandbox Code Playgroud)

sai*_*tam 6

这是解决方案:

这些丢失的消息有时可能会发生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()
Run Code Online (Sandbox Code Playgroud)