This may turn out to be a general Twisted question, but in my case it involves Autobahn, so here I am.
I desire that WebSocket traffic be handled over the same port as the static part of my Twisted web server. So I am using autobahn.resource.WebSocketResource like so:
resource = WebSocketResource(MyWebSocketServerFactory())
site = server.Site(site_root_resource)
In the client I connect to “ws://localhost:80/ws”, and this works well.
Time passes and I make updates to my MyWebSocketServerFactory class. I’d like to update this live while the Twisted process is running, so I’ve put a mechanism into place whereby I can ask the Twisted process to do this. Here is what it tries:
not shown: call transport.loseConnection() on all of the WebSocketServerProtocol instances
replacement_resource = WebSocketResource(MyWebSocketServerFactory())
NOTE: site.resource == site_root_resource from above
As far as I can tell this should be a safe operation, since putChild is just updating a dictionary.
Once I do this, however, clients are unable to reconnect. WebSocket reconnection attempts receive a 400 error code.
Is there a way to do what I would like? I am able to achieve this if the WebSocket part of the server is listening on a different port. I do current_ws_server.stopListening() and then listenWS(MyWebSocketServerFactory()).