SignalR not firing OnDisconnected

Viewed 3000

The problem: SignalR is not firing OnDisconnected on my webserver. It is very simple, but is not firing. I have tried:

  • Closing browser window
  • Navigating away
  • Reloading
  • Firing stop() on the SignalR-connection from client side.

public override Task OnDisconnected(bool stopCalled) {
    LogUtil.LogInfo("Disconn");
    return base.OnDisconnected(stopCalled);
}

I am using SignalR 2.2.0.0 and OnConnected is working just fine.
I have tried recommendations of doing at last one client call to make sure the connection is up and running.
I have searched all forums, SO, GIT etc etc and nothing solves my issue.

4 Answers

I also had issues with logging in OnDisconnect not functioning correctly. Like you, I figured that the OnDisconnect method was not being invoked.

But it turns out I was assuming that HttpContext.Current was available. (My logger uses it to pull some state information from the session.) When you think about it, expecting a current context doesn't make sense. You are disconnecting, after all! A null pointer exception was being thrown for HttpContext.Current, but I couldn't see it.

Once I removed the dependency on HttpContext.Current, I was able to log disconnects and send out alert emails.

In my case, I needed at least one chat.client.broadcast method to make it work ... otherwise, it would fire any other event but OnDisconnected method.

In my case, I had a hub I used only to call Server methods.

When I subscribed the javascript proxy client to at least one Server hub event, OnDisconnected method started firing.

hubProxy.on('myEvent', myHandler);
Related