SignalR Websocket Exception when closing client

拈花ヽ惹草 提交于 2019-12-04 03:05:10

Looks like a known issue which might be fixed in v3.

user3241900

Try providing a TimeSpan to the close method to give it time to actually close while waiting for any async processes which may be in progress:

public void Stop()
{
    _connection.Stop(new TimeSpan(1000));
}

This worked for me.

Ok guys, I know its lame. I am trying to shutdown signalr client when the mainwindow closes and I get a lot of exceptions that I can not catch and my process hangs. So i decided to kill the main process on form close.

private void OnClose(object sender, CancelEventArgs e){
        try
        {
            var process = Process.GetCurrentProcess();
            process.Kill();
            //HubConnection.Stop();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

}

I solved this problem with

connection.Reconnecting += connection.Stop;

Because I found the connection state is always reconnecting

If I understand correctly, you want to stop broadcasting message when client is disconnected. So my approach is use Connection Lifetime Events in SignalR. This is the official page : SignalRLifeTimeEvents

public override System.Threading.Tasks.Task OnDisconnected()
{
    //-- When any client will close the browser or page this event will fire

    return base.OnDisconnected();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!