WebSocketResource Reload/Reset

#1

This may turn out to be a general Twisted question, but in my case it involves Autobahn, so here I am. :slight_smile:

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_root_resource.putChild(“ws”, resource)

site = server.Site(site_root_resource)

reactor.listenTCP(80, site)

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

site.resource.putChild(“ws”, replacement_resource)

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.

If I stop and restart the Twisted process altogether, the JavaScript clients successfully reconnect.

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()).

0 Likes

#2

This may turn out to be a general Twisted question, but in my case it
involves Autobahn, so here I am. :slight_smile:

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_root_resource.putChild("ws", resource)
     site = server.Site(site_root_resource)
     reactor.listenTCP(80, site)

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

You mean, you change the code of the class?

If so, that would mean you are after "hot code replacement".

That's non-trivial to achieve under Python ... not like on Erlang/OTP where hot-code update is part of the run-time/system ..

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
     site.resource.putChild("ws", replacement_resource)

As far as I can tell this should be a safe operation, since putChild is
just updating a dictionary.

Not sure if that helps, but yOu might try this:

factory = MyWebSocketServerFactory()
factory.statFactory()
site.resource.putChild("ws", WebSocketResource(factory))

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/websocket/echo_service/echows/echoservice.py#L62
https://github.com/tavendo/AutobahnPython/issues/133

···

Am 19.02.2014 15:45, schrieb Jonathan Dobson:

Once I do this, however, clients are unable to reconnect. WebSocket
reconnection attempts receive a 400 error code.

If I stop and restart the Twisted process altogether, the JavaScript
clients successfully reconnect.

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()).

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes