SignalR .Net客户端:如何重新建立连接 [英] SignalR .Net Client: how to re-establish a connection

查看:571
本文介绍了SignalR .Net客户端:如何重新建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这篇文章

在某些应用程序中,您可能想要在连接丢失并且尝试重新连接超时后自动重新建立连接.为此,您可以从Closed事件处理程序(JavaScript客户端上已断开连接的事件处理程序)中调用Start方法.您可能需要等待一段时间才能调用Start,以免在服务器或物理连接不可用时过于频繁地执行此操作.以下代码示例适用于使用生成的代理的JavaScript客户端.

In some applications you might want to automatically re-establish a connection after it has been lost and the attempt to reconnect has timed out. To do that, you can call the Start method from your Closed event handler (disconnected event handler on JavaScript clients). You might want to wait a period of time before calling Start in order to avoid doing this too frequently when the server or the physical connection are unavailable. The following code sample is for a JavaScript client using the generated proxy.

当我从Closed事件调用Start方法时

When I call the Start method from the Closed event

connection.Closed += connection_Closed;
static void connection_Closed()
    {
        Console.WriteLine("connection closed");
        ServerConnection.Start().Wait();
    }

发生异常: 尚未建立连接.

Exception happened: the connection has not been established.

我希望它继续运行,直到服务器正常运行为止.不要抛出异常.我要如何做到这一点.

I want it continues until it success when the server is ok. Don't throw exception. How do I reach this.

有什么想法吗?

谢谢

推荐答案

在.net客户端上,您可以从关闭的事件处理程序中调用start方法. 如果服务器不可用,则应进行递归调用.

on .net client, you can call start method from closed event handler. if the server is unavailable, you should make a recursive call.

例如

_connection.Closed += OnDisconnected;
static void OnDisconnected()
{
    Console.WriteLine("connection closed");
    var t=_connection.Start()

    bool result =false;
    t.ContinueWith(task=>
    {
       if(!task.IsFaulted)
       {
           result = true;
       }
    }).Wait();

    if(!result)
    {
         OnDisconnected();
    }
}

这篇关于SignalR .Net客户端:如何重新建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆