© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
28 replies
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");
}
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
_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
ClientWebSocket#ReceiveAsync
method, it never advances beyond this point. Is there a trick I'm missing?
Establishing a connection | Revolt
To get started, you should have:
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Confusing ClientWebSocket.ReceiveAsync() behavior
C#CC# / help
3y ago
✅ ClientWebSocket concurrency question
C#CC# / help
4y ago
❔ Reading from ClientWebSocket with BinaryReader
C#CC# / help
3y ago