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.