.NET WebSockets forcibly closed despite keep-alive and activity on the connection

前端 未结 2 613
梦谈多话
梦谈多话 2020-12-28 15:53

We have written a simple WebSocket client using System.Net.WebSockets. The KeepAliveInterval on the ClientWebSocket is set to 30 seconds.

The connection is opened su

相关标签:
2条回答
  • 2020-12-28 16:14

    I set the KeepAliveInterval for the socket to 0 like this:

    theSocket.Options.KeepAliveInterval = TimeSpan.Zero;
    

    That eliminated the problem of the websocket shutting down when the timeout was reached. But then again, it also probably turns off the send of ping messages altogether.

    0 讨论(0)
  • 2020-12-28 16:37

    I ran into this issue this week. Your workaround got me pointed in the right direction, but I believe I've narrowed down the root cause.

    If a "Content-Length: 0" header is included in the "101 Switching Protocols" response from a WebSocket server, WebSocketClient gets confused and schedules the connection for cleanup in 100 seconds.

    Here's the offending code from the .Net Reference Source:

    //if the returned contentlength is zero, preemptively invoke calldone on the stream.
    //this will wake up any pending reads.
    if (m_ContentLength == 0 && m_ConnectStream is ConnectStream) {
        ((ConnectStream)m_ConnectStream).CallDone();
    }
    

    According to RFC 7230 Section 3.3.2, Content-Length is prohibited in 1xx (Informational) messages, but I've found it mistakenly included in some server implementations.

    For additional details, including some sample code for diagnosing ServicePoint issues, see this thread: https://github.com/ably/ably-dotnet/issues/107

    0 讨论(0)
提交回复
热议问题