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

2024-10-06 12:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

有人知道更好的解决办法吗?我试过这个,但一点也不管用:

^{pr2}$

Tags: fromimportselfasynciodef邮件argsprint
3条回答

解决方案如下:

这些丢失的消息有时可能与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()

你可以在这里检查我做的代码:Here。它用了漂亮的汤和干草。它通过进入Poloniex网站并等待一段时间,然后从网站收集数据,在我们的例子中是Trollbox。我也尝试了高速公路,这就是我得到的,但它看起来和你的代码完全一样,所以可能不会有任何改进。在

from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession,ApplicationRunner

#prints recieved message
def tamperMessage(message):
       print message



class MyComponent(ApplicationSession):

@inlineCallbacks
def onJoin(self, details):
    print("session joined")
    #gets message and calls tamperMessage function
    def gotMessage(type, messageNumber, username, message, reputation):
        tamperMessage(message)

    # 1. subscribe to a topic so we receive events
    try:
        yield self.subscribe(gotMessage,u'trollbox')
   except Exception as e:
       print("could not subscribe to topic:")

runner = ApplicationRunner(url=u"wss://api.poloniex.com", realm=u"realm1")

所以trollbox现在在wampwebsocket上不能正常工作,你得到一个断开连接的原因是由于不活动。在

如果您想检查它,可以查看网站源代码here,并查看第2440行,看到trollbox订阅被注释。在

相关问题 更多 >