SignalR .NET 客户端在网络连接丢失时不触发事件
SignalR .NET client not firing events when network connection is lost
我有连接到运行 SignalR 的 WebAPI 的 WPF 应用程序。一切正常,直到客户端丢失 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
感谢帮助
尝试订阅错误事件。根据 "how" 连接丢失,我认为其他一些事件不会被触发。
_hubConnection.Error += (e=>{ ... });
SignalR 上的传输类型自动设置为 ServerSentEvents 而不是 WebSockets(服务器管理员未将其打开)。事实证明,仅 使用 Websockets,当连接丢失时,我们可以在 .Net 客户端上获取与连接相关的事件。
根据http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr
WebSocket is the only transport that establishes a true persistent, two-way connection between client and server.
我有连接到运行 SignalR 的 WebAPI 的 WPF 应用程序。一切正常,直到客户端丢失 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 感谢帮助
尝试订阅错误事件。根据 "how" 连接丢失,我认为其他一些事件不会被触发。
_hubConnection.Error += (e=>{ ... });
SignalR 上的传输类型自动设置为 ServerSentEvents 而不是 WebSockets(服务器管理员未将其打开)。事实证明,仅 使用 Websockets,当连接丢失时,我们可以在 .Net 客户端上获取与连接相关的事件。
根据http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr
WebSocket is the only transport that establishes a true persistent, two-way connection between client and server.