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到服務器並獲得響應。