如果连接因不活动而超时,如何刷新 SignalR 连接
How do I refresh SignalR connection if connection timeouts from inactivity
我有一个使用 SignalR 的 angular 应用程序。当用户通过身份验证时,我会初始化与服务器的连接。我相信如果客户端在一定时间后处于非活动状态,它将断开连接。使用下面的代码,如果它因不活动而断开连接,我将如何重新连接?
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();
})
您可以观察断开连接并重新连接;
$.connection.yourHubName.client.disconnect(function () {
$.connection.hub.start();
});
我有一个使用 SignalR 的 angular 应用程序。当用户通过身份验证时,我会初始化与服务器的连接。我相信如果客户端在一定时间后处于非活动状态,它将断开连接。使用下面的代码,如果它因不活动而断开连接,我将如何重新连接?
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();
})
您可以观察断开连接并重新连接;
$.connection.yourHubName.client.disconnect(function () {
$.connection.hub.start();
});