Connection 检测来自客户端的信号器中的连接丢失

Connection 检测来自客户端的信号器中的连接丢失,connection,client,signalr,Connection,Client,Signalr,我正在将一个简单的应用程序连接到托管我的web应用程序的服务器。我的web应用程序使用signar2。一切都进展顺利,我的小应用程序可以与web应用程序同步,并接收从web应用程序发送的消息。但是,当网页更新或服务器重新启动并失去连接时,应用程序无法理解服务器的连接是否丢失。以下是我的代码: // initializing connection HubConnection connection; IHubProxy hub; connection = new HubConnection(ser

我正在将一个简单的应用程序连接到托管我的web应用程序的服务器。我的web应用程序使用signar2。一切都进展顺利,我的小应用程序可以与web应用程序同步,并接收从web应用程序发送的消息。但是,当网页更新或服务器重新启动并失去连接时,应用程序无法理解服务器的连接是否丢失。以下是我的代码:

// initializing connection
HubConnection connection;
IHubProxy hub;

connection = new HubConnection(serverAddress);
hub = connection.CreateHubProxy("MyPanel");
hub.On<string>("ReciveText", (msg) => recieveFromServer(msg));

您可以尝试类似的方法:

private void Connection_StateChanged(StateChange obj)
{

    if (obj.NewState == ConnectionState.Disconnected)
    {
        var current = DateTime.Now.TimeOfDay;
        SetTimer(current.Add(TimeSpan.FromSeconds(30)), TimeSpan.FromSeconds(10), StartCon);
    }
    else
    {
        if (_timer != null)
            _timer.Dispose();
    }
}

private async Task StartCon()
{
    await Connection.Start();
}

private Timer _timer;
private void SetTimer(TimeSpan starTime, TimeSpan every, Func<Task> action)
{
    var current = DateTime.Now;
    var timeToGo = starTime - current.TimeOfDay;
    if (timeToGo < TimeSpan.Zero)
    {
        return;
    }
    _timer = new Timer(x =>
    {
        action.Invoke();
    }, null, timeToGo, every);
}
来自信号员官方的例子

connection = new HubConnection(serverAddress);    
connection.Closed += Connection_Closed;

/// <summary>
/// If the server is stopped, the connection will time out after 30 seconds 
/// the default, and the `Closed` event will fire.
/// </summary>
void Connection_Closed()
{
 //do something
}
编辑

您可以尝试每隔15秒重新连接以下内容:

private void Connection_StateChanged(StateChange obj)
{

    if (obj.NewState == ConnectionState.Disconnected)
    {
        var current = DateTime.Now.TimeOfDay;
        SetTimer(current.Add(TimeSpan.FromSeconds(30)), TimeSpan.FromSeconds(10), StartCon);
    }
    else
    {
        if (_timer != null)
            _timer.Dispose();
    }
}

private async Task StartCon()
{
    await Connection.Start();
}

private Timer _timer;
private void SetTimer(TimeSpan starTime, TimeSpan every, Func<Task> action)
{
    var current = DateTime.Now;
    var timeToGo = starTime - current.TimeOfDay;
    if (timeToGo < TimeSpan.Zero)
    {
        return;
    }
    _timer = new Timer(x =>
    {
        action.Invoke();
    }, null, timeToGo, every);
}
private void Connection\u StateChanged(StateChange obj)
{
if(obj.NewState==ConnectionState.Disconnected)
{
var current=DateTime.Now.TimeOfDay;
SetTimer(当前添加时间跨度从秒(30)),时间跨度从秒(10),StartCon);
}
其他的
{
如果(_timer!=null)
_timer.Dispose();
}
}
专用异步任务StartCon()
{
等待连接。开始();
}
私人定时器;
私有void SetTimer(TimeSpan starTime、TimeSpan every、Func action)
{
var current=DateTime.Now;
var timeToGo=开始时间-当前时间的日期;
if(timeToGo
{
action.Invoke();
},null,timeToGo,every);
}

谢谢。我使用了你的解决方案,但它不起作用;当服务器停机时,连接状态变为“断开”。然后它尝试重新连接。短时间后,它将连接,但服务器无法检测到连接!:(我试过这个例子,效果很好,你可以在这里的信号器中获得更多关于理解和处理连接生存期事件的信息:@NacerFarajzadeh你是如何尝试重新启动连接的?实际上,我没有重新启动连接。我该怎么做?有什么安全的方法吗?@NacerFarajzadeh我用重新连接的方法编辑了我的答案。)发信号
private void Connection_StateChanged(StateChange obj)
{

    if (obj.NewState == ConnectionState.Disconnected)
    {
        var current = DateTime.Now.TimeOfDay;
        SetTimer(current.Add(TimeSpan.FromSeconds(30)), TimeSpan.FromSeconds(10), StartCon);
    }
    else
    {
        if (_timer != null)
            _timer.Dispose();
    }
}

private async Task StartCon()
{
    await Connection.Start();
}

private Timer _timer;
private void SetTimer(TimeSpan starTime, TimeSpan every, Func<Task> action)
{
    var current = DateTime.Now;
    var timeToGo = starTime - current.TimeOfDay;
    if (timeToGo < TimeSpan.Zero)
    {
        return;
    }
    _timer = new Timer(x =>
    {
        action.Invoke();
    }, null, timeToGo, every);
}