Reputation: 9232
I have an angular application using SignalR. When the user is authenticated, I initialize a connection to the server. I believe that if client is inactive after a certain amount of time, it will disconnect. With the code below, how would I reconnect if it disconnects from inactivity?
var initializeSockets = function(){
self.proxy = null;
var connection = $.hubConnection(ENV.socketEndpoint,{
qs: {token:User.getToken()},
logging: false,
useDefaultPath: false
});
self.proxy = connection.createHubProxy('messageCreatedEmitter');
self.proxy.on('onMessageCreated',function(data){
//the following is wrapped in a try catch
//because it will break in a browser, and
//will not allow the message to be emitted
try {
Alert.messageReceived();
} catch (e) {
}
$rootScope.$emit('Messages:messageReceived',data);
})
if($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected
|| $.connection.hub.state === undefined){
connection.start().done(function(){
$log.info('connected');
})
}
}
$rootScope.$on('User:userAuthenticated',function(event,user){
initializeSockets();
})
Upvotes: 0
Views: 4907
Reputation: 9806
You could watch for the disconnect and reconnect;
$.connection.yourHubName.client.disconnect(function () {
$.connection.hub.start();
});
Upvotes: 1