【发布时间】:2017-02-16 06:59:06
【问题描述】:
我在 SignalR 客户端中停止连接时遇到恶意问题。
下面是我的代码。
我用简单的方法开始连接
private void Initialize()
{
var hermesHubAddress = ConfigurationManager.AppSettings["HermesUrl"];
_hubConnection = new HubConnection(hermesHubAddress)
{
Credentials = CredentialCache.DefaultCredentials
};
_hubProxy = _hubConnection.CreateHubProxy("HermesHub");
_hubProxy.On<Notification>("Notify", Recieve_Message);
_hubConnection.Start().Wait();
_hubConnection.Error += HubConnectionOnError;
_hubConnection.StateChanged += HubConnectionOnStateChanged;
}
那么当客户端执行关闭连接的操作时
public void Stop()
{
_hubConnection.Stop();
_hubConnection.Dispose();
}
事件侦听器注意到连接状态发生了变化并调用了操作
private void HubConnectionOnStateChanged(StateChange stateChange)
{
if (stateChange.NewState == ConnectionState.Connected)
{
if (ConnectionEvent != null)
ConnectionEvent.Invoke(true);
}
else
{
if (ConnectionEvent != null)
ConnectionEvent.Invoke(false);
}
}
一切正常,客户端断开连接,但是当 SignalR 服务器以某种方式广播消息时 _hubProxy 自动重新连接到服务器并获得响应。
【问题讨论】: