简体   繁体   English

使用AutoBahn重新连接到websocket

[英]Reconnecting to a websocket using AutoBahn

I'm getting data from the Poloniex API by consuming their websocket API. 我通过使用他们的websocket API从Poloniex API获取数据。 I'm using the AutoBahn Python websocket library . 我正在使用AutoBahn Python websocket库

Currently, I'm using the following code to print ticket data as it becomes available. 目前,我正在使用以下代码打印票证数据,因为它可用。 This works well, but not for extended periods of time. 这种方法效果很好,但不能长时间使用。 Every 12-36 hours it stops producing output (I think when Poloniex restart their servers). 它每12-36小时停止产生输出(我认为当Poloniex重新启动它们的服务器时)。

Is there an elegant way to check if the ApplicationRunner is still connected, and to restart it if it disconnects? 是否有一种优雅的方法来检查ApplicationRunner是否仍然连接,并在断开连接时重新启动它? The onDisconnect is not triggering. onDisconnect未触发。 When I run the script, the process does not terminate when the socket stops receiving data, so I don't think that the event loop gets stopped. 当我运行脚本时,当套接字停止接收数据时,进程不会终止,因此我认为事件循环不会停止。

from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine
import asyncio
from datetime import datetime

class PoloniexComponent(ApplicationSession):
    def onConnect(self):
        self.join(self.config.realm)

    @coroutine
    def onJoin(self, details):
        def onTicker(*args):
            print("{}: {}".format(datetime.utcnow(), str(args)))
        try:
            yield from self.subscribe(onTicker, 'ticker')
        except Exception as e:
            print("Could not subscribe to topic:", e)

    def onDisconnect(self):
        asyncio.get_event_loop().stop()


def main():
    runner = ApplicationRunner("wss://api.poloniex.com:443", "realm1")
    runner.run(PoloniexComponent)

if __name__ == "__main__":
    main()

Since WebSocket connections are persistent, and no data is sent when there's no need, without actual traffic it's not discovered that the underlying connection has been killed (which is what happens when Poloniex Websocket server restart) - at least not quickly. 由于WebSocket连接是持久的,并且在没有需要时没有数据被发送,没有实际流量,因此未发现底层连接已被杀死 (这是Poloniex Websocket服务器重启时会发生的情况) - 至少不会很快。

To enable reacting quickly and deterministically to the disconnect , it is required to enable WebSocket auto pings on server side. 为了能够快速且确定地对断开连接做出反应 ,需要在服务器端启用WebSocket自动ping。 [ source ] [ 来源 ]

I don't know but suppose that polo have not activated this option ... 我不知道但是假设马球没有激活这个选项......

But you could build your "own" active ping version using something like this: 但是您可以使用以下内容构建“自己的”活动ping版本:

Create another ApplicationRunner instance (named "runner_watcher"), try to subscribe to a new topic with it and when it s done close the instance, then wait N second and restart the procedure, etc.. 创建另一个ApplicationRunner实例(名为“runner_watcher”),尝试使用它订阅一个新主题,当它完成后关闭实例,然后等待N秒并重新启动该过程等。

When the Exception ("Could not subscribe to topic") is raised on your "runner_watcher" instance, you may probably assume that server is down ..., so you force the stop of the primary ApplicationRunner and when server is back (no more Exception raised during execution of "runner_watcher", you may assume server is up again and restart the primary ApplicationRunner 当您的“runner_watcher”实例引发异常(“无法订阅主题”)时,您可能会认为服务器已关闭...,因此您强制停止主ApplicationRunner以及服务器何时返回(不再在执行“runner_watcher”期间引发异常,您可能会认为服务器再次启动并重新启动主ApplicationRunner

I agree that this approach is dirty (not elegant), but it may probably works 我同意这种方法很脏(不优雅),但它可能有效

Automatic reconnection was not yet implemented in autobahn in 2015 , but currently in the works. 2015年高速公路尚未实施自动重新连接,但目前正在进行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM