Connecting and Disconnecting from WebSockets in Python

#1

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.

0 Likes

#2

It's quite simple: just call sendClose() in your client. This will perform a proper WebSocket closing handshake and finally drop TCP
the connection.

···

Am 17.07.2012 18:18, schrieb Richard:

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.

0 Likes

#3

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?

···

On Tuesday, July 17, 2012 8:01:42 PM UTC+2, Tobias Oberstein wrote:

It’s quite simple: just call sendClose() in your client. This will
perform a proper WebSocket closing handshake and finally drop TCP

the connection.

Am 17.07.2012 18:18, schrieb Richard:

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.

0 Likes

#4

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

···

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?

0 Likes

#5

Excellent, Thanks for the example.

0 Likes

#6

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

···

On Tuesday, July 17, 2012 11:01:42 AM UTC-7, Tobias Oberstein wrote:

It’s quite simple: just call sendClose() in your client. This will
perform a proper WebSocket closing handshake and finally drop TCP

the connection.

Am 17.07.2012 18:18, schrieb Richard:

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.

0 Likes

#7

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

···

On Friday, January 18, 2013 1:22:16 AM UTC+1, Kevin Helton wrote:

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

0 Likes