SignalR 2.2.0集线器功能OnDisconnected

时间:2015-10-30 08:40:39

标签: c# signalr signalr-hub

以下是我在使用signalR和angularJS为前端开发简单聊天应用时遇到的两个问题:

  1. 在断开特定客户端时,对Ondisconnected集线器功能的调用平均在大约10-15分钟后到达服务器。

  2. 当呼叫最终到达服务器功能OnDisconnected(定义为仅使该客户端脱机)时,随后从所有其他连接的客户端收到多个呼叫,因此所有客户端都脱机,即使没有他们的关系存在问题。

  3. 以下是chatHub功能:

        public class ChatHub : Hub
        {
          public void UserOffline(string EMail)//makes user offline by the mailId
          {
            IEnumerable<ConnectionId> _connectionIds = database.GetConIds(EMail);
            bool successFlag = database.LogOff(EMail);
            if (successFlag == true)
            {
                foreach (ConnectionId item in _connectionIds)
                {
                    Clients.Client(item.connectionId).broadcastOffline();
                }
                IEnumerable<Client> _clients = database.GetAllClients();
                Clients.All.broadcastUsers(_clients,null);
            }
          }
    
          public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
           {
             if (stopCalled == false)
             {
                 string mailid = database.GetMailOfConId(Context.ConnectionId);
                 UserOffline(mailid);
             }
             return base.OnDisconnected(stopCalled);
           }
        }
    

1 个答案:

答案 0 :(得分:0)

我们可以在Global.aspx页面中设置Disconnection TimeOut,就像这样

  protected void Application_Start(object sender, EventArgs e)
   {
   // Make long polling connections wait a maximum of 110 seconds for a
   // response. When that time expires, trigger a timeout command and
   // make the client reconnect.
   GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

  // Wait a maximum of 30 seconds after a transport connection is lost
 // before raising the Disconnected event to terminate the SignalR        connection.
  GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

  // For transports other than long polling, send a keepalive packet every
  // 10 seconds. 
  // This value must be no more than 1/3 of the DisconnectTimeout value.
  GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

  RouteTable.Routes.MapHubs();
  }

有关详细信息,请参阅此[http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events#disconnecttimeout]