SignalR .NET客户端在网络连接丢失时不会触发事件

时间:2015-09-08 17:05:50

标签: asp.net wpf signalr signalr.client

我有WPF应用程序连接到运行SignalR的WebAPI。一切正常,直到客户端丢失Internet连接。 当它发生时,SignalR不会在客户端发起任何事件(StateChanged,Error,Reconnecting,Closed等)。

代码非常简单

 public HubConnection _hubConnection;
 IHubProxy _proxy;
 public async Task ConnectToHub(string hubUrl, string hubName)
 {
      _hubConnection = new HubConnection(HubURL);
      _hubConnection.Reconnecting += hubConnection_Reconnecting; 
      _hubConnection.Closed += _hubConnection_Closed;
      _hubConnection.StateChanged += _hubConnection_StateChanged;
      proxy = hubConnection.CreateHubProxy(hubName);

      await _hubConnection.Start();  

 }


   void _hubConnection_StateChanged(Microsoft.AspNet.SignalR.Client.StateChange obj)
    {
        throw new NotImplementedException();
    }

    void _hubConnection_Closed()
    {
        throw new NotImplementedException();
    }
    void _hubConnection_Reconnectig()
    {
        throw new NotImplementedException();
    }

SignalR 2.2.0版 谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

尝试订阅错误事件。根据连接丢失的“方式”,我不认为其他一些事件会被解雇。

 _hubConnection.Error += (e=>{ ... });

答案 1 :(得分:2)

SignalR上的传输类型自动设置为 ServerSentEvents ,而不是 WebSockets (服务器管理员未将其打开)。结果发现只有 的Websockets,当连接丢失时,我们可以在.Net-client上获得与连接相关的事件。

根据http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr

  

WebSocket是唯一一种在客户端和服务器之间建立真正持久的双向连接的传输方式。

相关问题