0
我有在SignalR客户端中停止连接的恶意问题。SignalR客户端HubConnection停止
这里我们用我的代码去。
我开始用简单的方法连接
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服务器广播时以某种方式asts消息_hubProxy autoreconnect到服务器并获得响应。