WebSocket的异常关闭不会返回ReceiveAsync

时间:2017-08-20 20:41:23

标签: c# websocket

我在服务器上有一个线程,用于处理通过WebSocket连接从客户端接收的数据:

    public HttpResponseMessage Get()
    {
        if (HttpContext.Current.IsWebSocketRequest)
        {
            HttpContext.Current.AcceptWebSocketRequest(ProcessWebSocket);
        }

        return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
    }

    private async Task ProcessWebSocket(AspNetWebSocketContext context)
    {
        WebSocket webSocket = context.WebSocket;    //Gets the current WebSocket object. 
        const int maxMessageSize = 1024;
        byte[] receivedDataBuffer = new byte[maxMessageSize];
        logger.Debug("WSC-New WebSocket connection");
        while (webSocket.State == WebSocketState.Open)
        {
            var timeOut = new CancellationTokenSource(70000).Token;
            WebSocketReceiveResult webSocketReceiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receivedDataBuffer), timeOut);
            //If input frame is cancelation frame, send close command. 
            if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close)
            {
                await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
                logger.Debug("WSC-Close WebSocket connection frame received");
            }
            else
            {
                //Process received data....
            }
        }
        logger.Debug("WSC-WebSocket closed, WebSocket state: {0}", webSocket.State.ToString());
    }

只要另一方正确关闭了WebSocket连接,我就会在结尾处收到记录器消息,说明websocket已关闭,这意味着该线程正确终止。如果客户端进入休眠状态或程序突然停止,则不会发生正常的WebSocket关闭。在这种情况下,webSocket.ReceiveAsync永远不会返回。我担心如果足够的这些线程启动但从未终止,我可能会有严重的内存泄漏。超时取消令牌应该在70秒后返回,但不会发生。如果我尝试在这种情况下从另一个线程访问webSocket对象,我会得到以下异常:无法访问已处置的对象。

如何从等待webSocket.ReceiveAsync(new ArraySegment(receivedDataBuffer),timeOut)返回;正确关闭这个帖子?

1 个答案:

答案 0 :(得分:2)

问题:

几乎任何连接突然关闭都会导致另一个套接字再也无法获取数据,这会导致套接字保持打开状态,就像你说的那样。

解决方案:

尝试使用Keep-Alive来决定连接超时的时间,另一个解决方案是使用可以被删除的单独线程,但是我不确定这对你有多好。

启动插座EX:

Thread X = new Thread(() => {
    Connect();
});
X.Start();

结束插座EX:

X.Abort();
// You could also force a Garbage Collection using
GC.Collect();