问题
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()
回答1:
Here is the solution:
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()
回答2:
You can check this code here i made: Here. It uses Beautiful soup and dryscape. It gets it by getting on Poloniex website and waiting for some time, then gathers data from website, in our case the Trollbox. I also tried with autobahn, and this is what i got, but it looks exactly like your code, so there would probably be no improvement.
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")
回答3:
so the trollbox is not functioning right now at the wamp web socket, the reason you are getting a disconnections is due to inactivity.
if you want to check it you can look at the website source here and look at line 2440 and see that the trollbox subscription is commented.
回答4:
Poloniex trollbox is over now ! You can have access to history here
来源:https://stackoverflow.com/questions/42436264/reading-messages-on-poloniex-trollbox-with-python-autbahn-or-other-socket-module