Search code examples
c#.netsignalr-hub

Will SignalR invoke OnDisconnected if user leaves the website


If the user navigates to a different site other than mine or clicks the X button to completely close the browser will the OnDisconnected still be called after a period of time?

Reason I'm asking is because I need to update the database when the user is gone so that it will not show the user as still in a room.

--Will the function below be invoked after a period of non-activity even if the user has completely closed out the browser?

    public override async Task OnDisconnected()
{
    if (!string.IsNullOrEmpty(this.Context.QueryString["acroomid"]))
    {
        string roomid = this.Context.QueryString["acroomid"],
               uid = this.Context.QueryString["uid"],
               isPro = this.Context.QueryString["isPro"];

        await Groups.Remove(Context.ConnectionId, roomid);

        ChatManager.LeaveChatRoom(uint.Parse(roomid), new Guid(Membership.GetUser().ProviderUserKey.ToString()));
        Clients.Group(roomid).gone(new Attendee(Context.ConnectionId, Membership.GetUser().UserName, bool.Parse(isPro)));
    };
}

Solution

  • According to this article here: http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events

    The server will wait until the disconnect timeout period has elapsed and then fire the OnDisconnected event. The provided scenario is that the client machine crashes or goes into sleep mode so the user closing the browser would be the same type of result.