javascript – OnDisconnected event in SignaIR doesn't work right


Guys, I developed a chat according to this link and everything worked fine. But the disconnected event doesn't work right.

What happens is that, according to the referral link, this event is triggered when the user closes the page, thus notifying online users that that user has left the chat. Only this event is not being called.

What happens is that if I close the tab (the page) this event is not triggered and does not notify the room that the user has left, and more, this username is saved and I cannot enter the chat with the same name, even when closing the tab and trying to open it again.

For me to be able to put the same name, I need to wait for the default timeout so that the entire site's cookie is deleted…

Could someone help me?

Remembering that all other events work, only the one that doesn't

Here are the codes referring to the event:

In the class that I extend HUB, the OnDisconnected method:

 public override Task OnDisconnected()
        var name = dic.FirstOrDefault(x => x.Value == Context.ConnectionId.ToString());
        string s;
        dic.TryRemove(name.Key, out s);
        return Clients.All.disconnected(name.Key);

– Here already an error. I can't override :

'SignalIRChatMVC.Hubs.MyHub.OnDisconnected()': no ​​suitable method found to override

the event in javascript

chat.client.disconnected = function (name) {
            //Calls when someone leaves the page
            $('#chats').append('<div class="border"><i>' + name + ' saiu da sala </i></div>');
            $('#onlineList div').remove(":contains('" + name + "')");
            $("#users option").remove(":contains('" + name + "')");

And here's the div you use to do online or offline user manipulations

<div style="height: 80%;">
<div id="chats" style="width: 80%; float: left;"></div>
<div id="onlineList" style="width: 19%; float: right; border-left: solid red 2px; height: 100%;">
    <div style="font-size: 20px; border-bottom: double">Usuários online</div>



Regarding the exception, the method to override the Hub class method has the signature

public virtual Task OnDisconnected(bool stopCalled)

Thus, your override will be:

public override Task OnDisconnected(bool stopCalled)

The method it mentions was removed in the latest version (see here ).

Life of a SignalR link

Regarding the end of the connection, in SignalR the connection can be terminated in the following ways:

  • The client calls the Stop method and a message is sent to the server signaling the end of the connection. The connection is terminated immediately.
  • The connection between client and server drops. The client will try to reconnect and the server will be waiting for the client. If connection attempts fail and the timeout period is exceeded, both the client and server stop trying.
  • If the client stops without having the opportunity to inform the server that it is going to shut down via the Stop method, the server waits for the client for a certain amount of time. If after that time the connection is not reestablished, the server will drop the pending connection.
  • If the server stops, the client will try to connect to the server for a certain period of time. If after this time the connection is not reestablished, the client will abandon the pending connection.

In your case, you should anticipate the first situation and invoke the Stop method on your client to make sure other users are informed that the client has left the chat.

call timeouts

Regarding timeouts , there are 3 different ones that control the connections in SignalR :

  • ConnectionTimeout: Time during which a connection remains open. The default is 110 seconds;
  • DisconnectTimeout: Time expected after a connection ends before invoking the disconnect event. The default is 30 seconds.
  • KeeepAlive: Expected time between each packet sent on a passive link. The default is 30 seconds. If you set this value to null , no KeepAlive packets are sent . If this time is active (not null) the ConnectionTimeout is ignored.

If you want to change these times, you can do it as follows:

[assembly:OwinStartup(typeof (StartupConfig))]

namespace Test
    public class StartupConfig
        public void Configuration(IAppBuilder app)
            GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(50);
            GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(50);
            GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(50);
Scroll to Top