Trying to build a custom chat UI in Blazor Server instead of using the standard widget. The docs say I can use the WebSocket API for real-time messaging. I’m following the auth flow from the platform guide, but the connection dies right after I send the auth message.
Here’s the C# code using System.Net.WebSockets:
var socket = new ClientWebSocket();
await socket.ConnectAsync(new Uri("wss://api.mypurecloud.com/api/v2/conversations/messaging/channels/ws"), CancellationToken.None);
var authPayload = JsonSerializer.Serialize(new {
type = "auth",
token = _accessToken
});
await socket.SendAsync(Encoding.UTF8.GetBytes(authPayload), WebSocketMessageType.Text, true, CancellationToken.None);
// Receive handshake response
var buffer = new byte[1024];
var result = await socket.ReceiveAsync(buffer, CancellationToken.None);
var response = Encoding.UTF8.GetString(buffer, 0, result.Count);
The response comes back as {"type":"auth-success"}. But the next ReceiveAsync call throws WebSocketException: The remote party closed the WebSocket connection without completing the close handshake. within 500ms. I haven’t sent a join message yet. Is there a keep-alive requirement I’m missing? Or does the server drop idle connections instantly? The docs don’t mention a timeout for the initial handshake phase.