简体   繁体   中英

How to use SignalR events to keep connection alive in the right way?

I am developing a real-time client-server application using SignalR, ASP.NET and C#. I am using localhost as host and VS2013.

My questions are:

  1. Why if I server, on web-client the " " event occurs?服务器,在网络客户端上会发生“”事件?

  2. The "Disconnect" event occurs after only. 发生。 How to reduce this time?

  3. I need the client to connect to server on start. The "Reconnect" event should occurs within only. 内发生。 If "Reconnect" interval time is over the client should connect as a new client. How to archive this goal?

Finally, I would like to ask - how to connection using in the right way ?连接?

I am using this code:

C#

public override Task OnDisconnected()
{
clientList.RemoveAt(nIndex);
Console.WriteLine("Disconnected {0}\n", Context.ConnectionId);
return (base.OnDisconnected());
}

public override Task OnReconnected()
{
Console.WriteLine("Reconnected {0}\n", Context.ConnectionId);
return (base.OnReconnected());
}

Javascript

$.connection.hub.reconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();

// Add the message to the page.
$('#discussion').append('Reconnected to server: ' + now + '</br>');
});

$.connection.hub.disconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();
// Add the message to the page.
$('#discussion').append('Disconnected from server: ' + now + '</br>');
});

After Connect output:

message received from server : Fri Feb 21 2014 10:53:02

After Close Server output:

Reconnected to server: Fri Feb 21 2014 10:53:22 <-- Why, if i close server ???
Disconnected from server: Fri Feb 21 2014 10:53:53 <-- Why 40+ seconds after the server is closed ?

1. After I server, on web-client the " " event occurs and the " " event occurs only after. 服务器后,在 Web 客户端上,“”事件发生,而“”事件仅在此之后发生。 Why?

SignalR cannot tell the difference between closing the server and restarting the server. For this reason, when the server shuts down the client will start to try to reconnect in case the server is actually restarting.

2. The "Disconnect" occurs after of unknown "Reconnect". 以上发生“断开连接”。 How to reduce this time?

This 30 second timeout can be modified via the DisconnectTimeout property .

3. I need the client to connect to the server on start. The "Reconnect" should occurs within only. 内发生。 If "Reconnect" interval time is over the client should connect as new client.

You should start the connection on the disconnected event , preferably after a timeout to reduce server load if it restarts.

$.connection.hub.disconnected(function() {
    setTimeout(function() {
        $.connection.hub.start();
    }, 5000); // Re-start connection after 5 seconds
});

The entire Understanding and Handling Connection Lifetime Events in SignalR article is probably relavent to your question.

This method is for when you do not want to change the server configure ; javascript example code :

connection.serverTimeoutInMilliseconds = 1000 * 60 * 10; // for  10 minute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM