Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignalR client JS doesn't reconnect if fails when starting first time #1962

Closed
bborad opened this issue Apr 29, 2013 · 6 comments
Closed

SignalR client JS doesn't reconnect if fails when starting first time #1962

bborad opened this issue Apr 29, 2013 · 6 comments

Comments

@bborad
Copy link

bborad commented Apr 29, 2013

Hi

My signalR client in my web page tries to connect to the server in a desktop application.

Issue is when client (web page) tries to connect to the server first time and if the server is not online it fails and doesn't reconnect. But if it connects in first time then after, if server drops off, it automatically tries to reconnect.

Here's code example (client side)

var conn = $.hubConnection('url');
var chub = conn.createHubProxy('myhub');

chub.on('messageReceived', function(msg){
$('#log').after('Message Received : ' + msg);
});

conn.stateChanged(function (state){
if(state.newState == $.signalR.connectionState.reconnecting){
$('#log').after('Reconnecting');
}
else if(state.newState == $.signalR.connectionState.connected){
conn.start().done(function(){
$('#log').after('connected');
}).fail(function(){
$('#log').after('Connection failed.');
});
}
});

conn.start().done(function(){
$('#log').after('connected');
}).fail(function(){
$('#log').after('Connection failed.');
});
});

@davidfowl
Copy link
Member

That's by design. Reconnect only works if you're already connected. But if the connection failed to start in the first place then it won't auto reconnect. If you'd like to always retry then you can write code like this:

  connection.disconneted(function() {
     connection.log('Connection closed. Retrying...');
     setTimeout(function() { connection.start(); }, 5000);
  });

@bborad
Copy link
Author

bborad commented Apr 30, 2013

Thanks,

Any particular reason for that?

Just want to know for knowledge.

Thanks again

@davidfowl
Copy link
Member

Because it's a more natual API. If you connect to a service that is down chances are something is really wrong. If you were connected at some point and the connection dropped , signalr will retry for a little bit then give up and disconnect. They are very different scenarios and we try to keep the connection up if there's blips in the network but not when failing to start the connection altogether. That behavior is more predictable.

@bborad
Copy link
Author

bborad commented Apr 30, 2013

Oh... So if the server goes off and doesn't come back, then after trying for a little bit, signalr will give up and won't try to reconnect.

To keep trying continuously, I have to use disconnected event.

@davidfowl
Copy link
Member

yes

@bborad
Copy link
Author

bborad commented May 7, 2013

One more weird question, why we need timeout inside disconnected method?.

what I understand is, we have added a callback method that gets called when connection is disconnected: "conn.disconncted()".

For the first time we call conn.start() to try to connect to the server. but if the server is not available, it will call disconnected method. Now inside this disconnected method, if we again call the conn.start() method, shouldn't it do the same thing again. i.e. shouldn't it try to connect to the server and if fails call disconnected method again ( and continuing the loop util its connected.).

I try to test this but it's not behaving in this way (it works is I set timeout), so just want to understand why.

Really appreciate your help.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants