DEV Community

Cover image for SignalR core python client (V): Reconnecting Client

SignalR core python client (V): Reconnecting Client

Andrés Baamonde Lozano on June 16, 2019

Intro These days I have been working on a new feature. The client reconnection. To know if a client has been disconnected we must set a ...
Collapse
 
mirstation2001 profile image
Javier Barreiro

How to get the return value of the .send function (async).
If in the Hub (signalr hub in c#), there is a function, for example public string FOO() that returns a value, how to get that value calling to
hub_connection.send("FOO", [])

.send does not seem to be ASYNC function
Thanks

Collapse
 
mandrewcito profile image
Andrés Baamonde Lozano

Hi,

This functionality solves your problem? github.com/mandrewcito/signalrcore...
If does not, let me know

thank you!

Collapse
 
mirstation2001 profile image
Javier Barreiro

Not sure. Is it the on_invocation parameter? I tried but did not understand how to get the return value.
Do you have an example?
Thanks!

Thread Thread
 
mandrewcito profile image
Andrés Baamonde Lozano

Yes,

Here are 2 examples of using the callback. There is no return value, websocket methods are not synchronous. To listen from a callback , you must wait for a message with your invocation id.

github.com/mandrewcito/signalrcore...
github.com/mandrewcito/signalrcore...

Collapse
 
samy0392 profile image
Samer Abbas

How to reconnect when socket gets closed by server?

Collapse
 
mandrewcito profile image
Andrés Baamonde Lozano

Hi again, i have just published a version with includes automatic reconnect, i delayed a lot, because a was fixing a problem with py2 compatibility.

pypi.org/project/signalrcore/0.7.3/

Collapse
 
samy0392 profile image
Samer Abbas

Reconnect is now working even after the socket closes!

When websocket re-opens after closing, I see this error:
ERROR error from callback >: Expecting value: line 1 column 1 (char 0)

Websocket stays open after this error and I am still able to receive messages through signalr. So this is probably some kind of warning.

Thread Thread
 
mandrewcito profile image
Andrés Baamonde Lozano

I think that is an error related with auth, if an errors occuurs on the login function library encapsulates it an try login later:

error image

do you refer to that error?

I think that my next exteps on the library must be improving logging and documentation to prevent this kind of things.

Thread Thread
 
samy0392 profile image
Samer Abbas • Edited

Yes after that error, it reconnects and websocket opens again. After websocket opens, then I see this error:

"ERROR error from callback <bound method BaseHubConnection.on_message of signalrcore.hub.base_hub_connection.BaseHubConnection object at 0xb5954410: type"

Thread Thread
 
mandrewcito profile image
Andrés Baamonde Lozano

That error was solved in the 0.74. Was related with the connection handshake. Yesterday I uploaded
the fix. The new version is available since then.

Thread Thread
 
samy0392 profile image
Samer Abbas

Awesome!
Thank you!

Collapse
 
samy0392 profile image
Samer Abbas

Thank you! I will test it.

Collapse
 
samy0392 profile image
Samer Abbas

Hi Andrés,

How can I send a signalr message everytime websocket reconnects after a disconnect?
I want to send a message to the server when I reconnect.
How can I do this?

Thank you

Collapse
 
mandrewcito profile image
Andrés Baamonde Lozano

On the next version, Callbacks for on_connect and on_disconnect will be included. This change would solve your problem right?

Collapse
 
samy0392 profile image
Samer Abbas

Yes perfect! Thank you so much!