1

I have run into a problem where my websocket connection is functioning as expected locally, but not when deployed to azure app service. (.NET Core 3.0)

I am able to receive messages of any size locally, but the messages cap out at 4088 byte when deploying to azure.

Code example:

await Receive(socket, async (result, buffer) =>
{
    Console.WriteLine("Message size: " + result.Count);
    message = Encoding.UTF8.GetString(buffer, 0, result.Count);
});

private async Task Receive(WebSocket socket, Action<WebSocketReceiveResult, byte[]> handleMessage)
{
    try
    {
        var buffer = new byte[1024 * 16];
        while (socket.State == WebSocketState.Open)
        {
            WebSocketReceiveResult result = null; 
            using (var cts = new CancellationTokenSource(1200000))
            {
                result = await socket.ReceiveAsync(
                    buffer: new ArraySegment<byte>(buffer),
                    cancellationToken: cts.Token
                    );
            }
            handleMessage(result, buffer);
        }
    }
    catch (Exception ex)
    {
        Log.Error(ex, "Exception occured when receiving message async");
    }
}

Is there any kind of limit that can be changed in the app service?

I have already tried to set up a remote client sending messages both to azure app service and my local environment. This is only an issue with azure app service.

1 Answer 1

0

Fixed with:

private async Task Receive(WebSocket webSocket, Action<string> handleMessage)
{
    try
    {
        while (webSocket.State == WebSocketState.Open)
        {
            var compoundBuffer = new List<byte>();
            WebSocketReceiveResult messageReceiveResult = null;
            byte[] buffer = new byte[4 * 1024];

            do
            {
                using (var cts = new CancellationTokenSource(1200000))
                {
                    messageReceiveResult = await webSocket.ReceiveAsync(
                    new ArraySegment<byte>(buffer),
                    cts.Token
                    );
                }

                if (messageReceiveResult.MessageType == WebSocketMessageType.Text)
                {
                    byte[] readBytes = new byte[messageReceiveResult.Count];
                    Array.Copy(buffer, readBytes, messageReceiveResult.Count);
                    compoundBuffer.AddRange(readBytes);
                }
            } while (!messageReceiveResult.EndOfMessage);

            string message = Encoding.UTF8.GetString(compoundBuffer.ToArray());
            handleMessage(message);
        }
    }
    catch (Exception ex)
    {
        Log.Error(ex, "Exception occured when receiving message async");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.