使用Python autbahn或其他套接字模块在Poloniex Trollbox上阅读消息吗? [英] Reading Messages on Poloniex Trollbox with Python autbahn or other socket module?

查看:94
本文介绍了使用Python autbahn或其他套接字模块在Poloniex Trollbox上阅读消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Poloniex不会将所有消息都返回到我的套接字.我使用以下代码阅读消息,有时我会得到连续的消息编号,但有时会丢失大约10条消息:

Poloniex doesn't return every message to my socket. I read the messages with the following code and sometimes I get continuous message numbers, but sometimes there are like 10 messages missing:

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)

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

Does anybody know a better solution? I tried this one, but it doesn't work at all:

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}'.

These missing messages might happen sometimes with the WAMP API. It is due to inherent scalability problems with the routing software, and Poloniex is working on a pure WebSockets API (currently used by the web interface, but lacking documentation) to replace it. The url for the new websocket server is wss://api2.poloniex.com:443 and to connect to trollbox messages you would need to send the message: '{"command" : "subscribe", "channel" : 1001}'.

这是示例代码,使用起来更容易:

Here is an example code, it is much easier to work with:

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 autbahn或其他套接字模块在Poloniex Trollbox上阅读消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆