简体   繁体   English

如何使高速公路/扭曲的websocket服务器重新启动/异常后保持正常运行

[英]how to make autobahn / twisted websocket server restart / stay up after exception

I have a websocket server using autobahn where it runs some code onMessage. 我有一个使用高速公路的websocket服务器,它在onMessage上运行一些代码。

during this code an exception happens and I perform a try/except clause so I can log the error, it logs correctly. 在这段代码中,发生异常,我执行了try / except子句,这样我就可以记录错误,并且记录正确。 However the connection to the client(javascript) disconnects. 但是,与客户端(javascript)的连接断开。

The problem is that if I load a new page it does not reconnect to the websocket server. 问题是,如果我加载新页面,它不会重新连接到websocket服务器。 I attempted to put the code block to start the server in a while loop but the client does not connect succesfully. 我试图将代码块放入while循环中以启动服务器,但客户端未成功连接。

if __name__ == '__main__':

    import sys

    from twisted.python import log
    from twisted.internet import reactor, task, threads
    logging.basicConfig(filename='/var/log/myerror.log', level=logging.ERROR)

    log.startLogging(sys.stdout)

    factory = WebSocketServerFactory(u"ws://127.0.0.1:9000", debug=False)
    factory.protocol = MyServerProtocol
    factory.setProtocolOptions(maxConnections=50)

    reactor.listenTCP(9000, factory)
    reactor.run()

Does anyone know how to make it so even if the code in 'onMessage' has an exception, the websocket server will continue to accept connections and give other clients the opportunity to run? 有谁知道如何做到这一点,即使'onMessage'中的代码有异常,websocket服务器仍将继续接受连接并为其他客户端提供运行的机会?

Found the problem. 找到了问题。 I had placed 'reactor.callFromThread(reactor.stop) ' elsewhere in the code and it was killing the reactor. 我在代码中的其他地方放置了“ reactor.callFromThread(reactor.stop)”,它正在杀死反应堆。

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

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