0

I have .NET code with websocket connectivity. When I'm testing this in the emulator, it is working perfectly.

But when I deploy this to Azure and add this as a skill in copilot, when I trigger it, for the first message, the websocket should be initiated and from second message, messages should be sent. But in copilot, for the first message, even though it is initiating the websocket, I get a timeout response. Sending and receiving of messages is happening. But still I'm getting the timeout error.

For code context, I have put all my code in OnMessageActivity method in DefaultActivityHandler:

private async Task StartWebSocketClientAsync(string thread, ClientWebSocket client, ITurnContext<IMessageActivity> turnContext)
{
        using (client)
        {
            try
            {
                string uri = "wss://echo.websocket.org/";
                await client.ConnectAsync(new Uri(uri), CancellationToken.None);
                OnWebSocketConnect(thread, "Connected to server");
               
                _ = Task.Run(async () => await ReceiveMessagesAsync(client, turnContext));

                while (client.State == WebSocketState.Open)
                {
                    await Task.Delay(100);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
}

private async Task ReceiveMessagesAsync(ClientWebSocket client, ITurnContext<IMessageActivity> turnContext)
{
        var buffer = new byte[1048576];
        var messageBuilder = new StringBuilder();

        try
        {
            while (client.State == WebSocketState.Open)
            {
                WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
               //await turnContext.SendActivityAsync(" In Receiveasync " + result);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    try
                    {
                        await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
                    }
                    catch (WebSocketException wex)
                    {
                        Console.WriteLine($"WebSocket Close Error: {wex.Message}");
                    }
                    break;
                }
                else
                {
                    messageBuilder.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));

                    if (result.EndOfMessage)
                    {
                        string completeMessage = messageBuilder.ToString();
                        string mes = "Received the message" + completeMessage;

                        if (!string.IsNullOrEmpty(completeMessage))
                        {
                            turnContext.SendActivityAsync(completeMessage);
                        }
                        messageBuilder.Clear(); // Clear for the next message
                    }
                }
            }
        }
        catch (Exception ex)
        {
            await turnContext.SendActivityAsync(ex.ToString());
        }
}

What can I do to avoid the timeout error?

I have tried deploying to azure and faced the timeout issue. So I avoided deploying and used the ngrok url to run locally and added this as a skill in copilot. But the issue is still the same.

2
  • Go to Azure Portal > App Service > Configuration > General Settings and enable WebSocket. Keep the connection alive by sending periodic pings: while (client.State == WebSocketState.Open) { await client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("ping")), WebSocketMessageType.Text, true, CancellationToken.None); await Task.Delay(30000); } Retry WebSocket connection if it fails: int retries = 0; while (client.State != WebSocketState.Open && retries++ < 3) { await client.ConnectAsync(new Uri(uri), CancellationToken.None); await Task.Delay(1000); } Commented Nov 25, 2024 at 14:50
  • The websocket is enabled in azure settings. Added the pings. Still the same. Also identified that the while loop in StartWebSocketClientAsync method is keeping the process flow within the method till copilot gives the time out issue. But If I take out that while loop, the websocket is closing. If i keep, the error is being same Commented Nov 26, 2024 at 16:56

0

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.