Ondisconnected event in Signair does not work right

Asked

Viewed 102 times

3

Guys, I’ve developed a chat accordingly with this link and it all worked out well. But the Disconnected event doesn’t work right.

What happens is that, according to the reference link, this event is triggered when the user closes the page, and 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 warn the room that the user left, and more, this username is saved and I can not enter the chat with the same name, even when closed the tab and trying to open again.

In order for me to be able to put the same name, I need to wait for the default timeout so that all cookie of the site is deleted...

I wonder if someone could help me ?

Remembering that all other events work, only that

Here the codes referring to the event:

In the class extending 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 use the 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 is the div that uses to do the manipulations of user online or offline

<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>
</div>

1 answer

1


Exception

Concerning the exception, the method for doing override of the class method Hub has the signature

public virtual Task OnDisconnected(bool stopCalled)
{...}

Thus, your override will stay:

public override Task OnDisconnected(bool stopCalled)
{...}

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

Life of a Signalr connection

With regard to the termination of the connection, in SignalR the link can be terminated in the following ways:

  • The client calls the method Stop and a message is sent to the server signalling the end of the connection. The connection is terminated immediately.
  • The connection between the client and the server drops. The client will try to reconnect and the server will be waiting for the client. If connection attempts fail and the period of timeout is exceeded, both client and server stop trying.
  • If the client stops without having opportunity to inform the server that it will shut down through the method Stop, the server waits for the client for a certain time. If at the end of that time the connection is not restored, the server abandons the pending connection.
  • If the server stops, the client will try to connect to the server for a certain period of time. If at the end of that time the connection is not restored, the customer leaves the pending connection.

In its case, it should foresee the first situation and invoke the method Stop on your customer to make sure other users are informed that the customer has left the chat.

Connection timeouts

As regards the timeouts, there are 3 distinct controls on the connections in SignalR:

  • Connectiontimeout: Time during which a connection remains open. The default is 110 seconds;
  • Disconnecttimeout: Expected time after a connection has ended before calling the disconnection event. The default is 30 seconds.
  • Keeepalive: Expected time between each package sent on a passive link. The default is 30 seconds. If you set this value to null, no packages are sent from KeepAlive. If this time is active (not zero) 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)
        {
            app.MapSignalR();
            GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(50);
            GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(50);
            GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(50);
        }
    }
} 
  • So if I change the term override to virtual, it would work ? In a good way ?

  • 1

    Yes, the virtual is as defined in the class base. When does the override just replace the virtual for override. (I am editing the answer regarding the means of terminating the link on SignalR)

  • Our @Omni, thank you very much ! I’m some time breaking my head to solve ! I’m waiting for your edition !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.