如果连接超时不活动,如何刷新SignalR连接

时间:2015-12-15 05:11:11

标签: angularjs signalr signalr-hub signalr.client

我使用SignalR进行角度应用。用户通过身份验证后,我会初始化与服务器的连接。我相信如果客户端在一段时间后处于非活动状态,它将断开连接。使用下面的代码,如果它与不活动断开连接,我将如何重新连接?

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();
 })

1 个答案:

答案 0 :(得分:1)

您可以注意断开连接并重新连接;

$.connection.yourHubName.client.disconnect(function () {
    $.connection.hub.start();
});
相关问题