简体   繁体   中英

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.

during this code an exception happens and I perform a try/except clause so I can log the error, it logs correctly. However the connection to the client(javascript) disconnects.

The problem is that if I load a new page it does not reconnect to the websocket server. I attempted to put the code block to start the server in a while loop but the client does not connect succesfully.

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?

Found the problem. I had placed 'reactor.callFromThread(reactor.stop) ' elsewhere in the code and it was killing the reactor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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