Connecting and Disconnecting from WebSockets in Python

1,455 views
Skip to first unread message

Richard

unread,
Jul 17, 2012, 12:18:17 PM7/17/12
to autob...@googlegroups.com
Hi,

I am trying to get Autobahn.Python to work with connecting and disconnecting without having to restart the program.

I have taken the default message based tutorial and modified it to send messages then attempt to disconnect the websocket. In the protocol class, when I have sent a message to the server and the server sends the response, I call "dropConnection" or "sendClose" methods (not sure which is more correct). The call realizes in a connection lost signal to the server and it disconnects.

However, now the client is stuck waiting on the call "reactor.run()". If I call "reactor.stop", the "run" returns. But now I can't call "reactor.run" anymore as it throws an exception that the reactor cannot be restarted.

How do you get Autobahn to drop and reconnect websocket connections at some later date?


Thanks.

Tobias Oberstein

unread,
Jul 17, 2012, 2:01:42 PM7/17/12
to autob...@googlegroups.com, Richard
It's quite simple: just call sendClose() in your client. This will
perform a proper WebSocket closing handshake and finally drop TCP
the connection.

Richard

unread,
Jul 18, 2012, 10:04:17 AM7/18/12
to autob...@googlegroups.com, Richard
Thanks. But how do you get it to reconnect?

I assume calling connectWS(factory) again? But if reactor has locked the main look, this would imply a thread or something needing to call it?

Tobias Oberstein

unread,
Jul 18, 2012, 11:06:05 AM7/18/12
to autob...@googlegroups.com, Richard
Am 18.07.2012 16:04, schrieb Richard:
> Thanks. But how do you get it to reconnect?
>
> I assume calling connectWS(factory) again? But if reactor has locked the
> main look, this would imply a thread or something needing to call it?

No. There isn't such thing as "reactor lock" with Twisted. Everything
runs in 1 thread (normally), so there isn't a need for locks.

If you want to reconnect to the same server, just issue a connect() like
here:

https://github.com/tavendo/AutobahnTestSuite/blob/master/autobahntestsuite/autobahntestsuite/testee.py#L87

If you want to connect to a different server, you create a new factory
and do connectWs(), again i.e. in clientConnectionLost.

Note, that this is all less an AutobahnPython, but more of a Twisted
question .. it takes a little rethinking doing stuff in an asynchronous
networking framework.

Hope that helps,
Tobias

Richard

unread,
Jul 19, 2012, 4:44:55 AM7/19/12
to autob...@googlegroups.com, Richard
Excellent, Thanks for the example. 

Kevin Helton

unread,
Jan 17, 2013, 7:22:16 PM1/17/13
to autob...@googlegroups.com, Richard
If you don't mind, how can I call sendClose() through the websocket upon a page refresh? I'm thinking window.onbeforeunload -> sendClose(). Any suggestions? Right now, all registered clients will remain and build up over time.

Thanks,

Kevin
Message has been deleted

Daniel F.

unread,
Jan 18, 2013, 10:51:05 AM1/18/13
to autob...@googlegroups.com, Richard
Performing a sendClose in the onbeforeunload event handler is the best way to do it. Registering the onbeforeunload event via addEventListener ensures that other event listeners don't get overridden.

Sorry to all for my other 2 posts prior to this one, I was a bit light-minded.

Kind regards,
Daniel


Reply all
Reply to author
Forward
0 new messages