C#C
C#3y ago
Kiel

ClientWebSocket hangs on ReceiveAsync, never continues

I will concede I am brand new to working with websockets. I'm following this Establishing a connection guide which uses websockets for connection. One of the recommended ways for initiating a connection is to connect normally, and then send an Authenticate event to the server upon which you will receive either an Error or Authenticated event.

In an obviously rough draft attempt to accomplish this feat:
try
{
    var node = await restClient.ApiClient.QueryNodeAsync(cancellationToken);
    await _ws.ConnectAsync(new Uri(node.Ws), cancellationToken);
    var model = new AuthenticateSendEventApiModel(token.RawToken);

    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream) {AutoFlush = true})
    {
        var json = JsonConvert.SerializeObject(model);
        await writer.WriteAsync(json);
        await _ws.SendAsync(stream.ToArray(), cancellationToken);
    }

    var stream2 = await _ws.ReceiveAsync(cancellationToken); // hangs here
    logger.LogInformation("Length: {Length}", stream2.Length);
}
catch (Exception ex)
{
    logger.LogError(ex, "Failed");
}


my _ws is a class which wraps ClientWebSocket, and I can confirm my data is being sent (at least, no exception is thrown), but then when I try to receive, it never advances. Even stepping into it and breakpointing the actual ClientWebSocket#ReceiveAsync method, it never advances beyond this point. Is there a trick I'm missing?
To get started, you should have:
Was this page helpful?