SignalR 2.2.0集线器功能OnDisconnected

本文关键字:OnDisconnected 功能 SignalR 集线器 | 更新日期: 2023-09-27 18:19:56

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

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

  2. 当调用最终到达服务器函数OnDisconnected(定义为仅使该客户端离线)时,随后会从所有其他连接的客户端接收到多个调用,因此所有客户端都会离线,即使它们的连接没有问题。

以下是聊天中心的功能:

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

SignalR 2.2.0集线器功能OnDisconnected

我们可以像这样在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]