OnDisconnected event in SignaIR does not work right

3

Well, I've developed a chat according with this link and everything worked fine. But the disconnected event does not work right.

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

What happens is that if I close the tab this event is not triggered and does not tell the room that the user left, and more, that username is saved and I can not chat with the user. same name, even when you closed the tab and trying to open it again.

So that I can put the same name, I need to wait for the default timeout so that the entire site cookie is deleted ...

Could anyone help me?

Remembering that all other events work, only the one that does not

Here are the codes for the event:

In the class I'm 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 is an error. I can not 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's the div you use to do user manipulations 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>

    
asked by anonymous 28.10.2014 / 14:49

1 answer

1

Exception

For the exception, the method to make override the method of class Hub has the signature

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

So, your override will be:

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

The method you mention has been removed in the latest version (see here ).

Life of a SignalR connection

For the end of the connection, in% with% the connection can be terminated in the following ways:

  • The client calls the SignalR method and a message is sent to the server signaling 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 the connection attempts fail and the timeout period is exceeded, both the client and the server stop trying.
  • If the client stops without having the opportunity to inform the server that will disconnect through the Stop method, the server waits for the client for a certain time. If the connection is not restored after that time, the server leaves the connection pending.
  • If the server stops, the client will try to connect to the server for a certain period of time. If the connection is not restored after that time, the client leaves the connection pending.

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

Link timeouts

For Stop , there are 3 distinct ones that control connections in timeouts :

  • ConnectionTimeout: The length of time a connection remains open. default is 110 seconds;
  • DisconnectTimeout: The expected time after a connection terminates before invoking the disconnect event. default is 30 seconds.
  • KeeepAlive: Expected time between each packet sent on a passive connection. The default is 30 seconds. If you set this value to SignalR , null packets are not sent. If this time is active (not null) the KeepAlive 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);
        }
    }
} 
    
29.10.2014 / 22:49