I have a Xamarin app that communicates using WebSocket. On the client side, I am referencing the ClientWebSocket.
code:
using (var client = new ClientWebSocket() { Options = { KeepAliveInterval = new TimeSpan(0,0,3,0) }})
{
    try
    {
        await client.ConnectAsync(requestUri, cancellationToken);
        do
        {
            WebSocketReceiveResult result = null;
            var buffer = new byte[ReceiveChunkSize];
            var message = new ArraySegment<byte>(buffer);
            do
            {
                result = await client.ReceiveAsync(message, cancellationToken);
                var receivedMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
                data.Append(receivedMessage);
                if (result.MessageType != WebSocketMessageType.Text)
                    break;
            } while (!result.EndOfMessage);
            TranslateData(data);
        } while (client.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested);
The application keeps throwing intermittently:
 System.Net.WebSockets.WebSocketException (0x80004005): The remote
 party closed the WebSocket connection without completing the close
 handshake.
The server has a test website where it seems to function as expected. It seems the problem is on the client. The value in the client.State is Open.
User contributions licensed under CC BY-SA 3.0