C
C#13mo 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");
}
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?
Establishing a connection | Revolt
To get started, you should have:
15 Replies
JakenVeina
JakenVeina13mo ago
this code is all client-side? are you quite sure the server-side is good? is it definitely sending something for you to read? cause that's what ReceiveAsync() is waiting for either some data to read, or the socket to close
Kiel
KielOP13mo ago
I'm just following the docs on how it's supposed to be working. Maybe I'm sending my data incorrectly? I'm just writing raw JSON to the buffer and then sending that, I guess I should check what's actually being sent I can confirm the server is actually functioning as the Revolt client exists and is working
JakenVeina
JakenVeina13mo ago
let's see it
Kiel
KielOP13mo ago
Well, I guess I will first fix the fact my event type is being serialized as an int instead of string.
No description
Kiel
KielOP13mo ago
A little sidetracked - is there a way to define a StringEnumConverter for ALL enum fields in my models? it's a little annoying to have to put them on each individual model oh nvm, figured it out
JakenVeina
JakenVeina13mo ago
yes, through the serializer options I don't remember specifically, but I know I've encountered this before
Kiel
KielOP13mo ago
services.AddSingleton(new JsonSerializer
{ Converters = { new OptionalJsonConverter(), new StringEnumConverter() }, ContractResolver = new OptionalJsonContractResolver() });
services.AddSingleton(new JsonSerializer
{ Converters = { new OptionalJsonConverter(), new StringEnumConverter() }, ContractResolver = new OptionalJsonContractResolver() });
(plus my other stuff obviously)
JakenVeina
JakenVeina13mo ago
yeah
Kiel
KielOP13mo ago
NICE, that was the issue all along
No description
Kiel
KielOP13mo ago
after like 40+ models written and I didn't realize it was never serializing enums correctly lol
JakenVeina
JakenVeina13mo ago
ah, so it was throwing an exception deserializing, and you weren't seeing that?
Kiel
KielOP13mo ago
actually the other way around. My Authenticate data was malformed since it was not serialized correctly, so it (the server) never sent anything in response So then after that....just loop ReceiveAsync in the background to dispatch events?
JakenVeina
JakenVeina13mo ago
well, that's what I mean an exception was happening when the server tried to deserialize the data it sounds like you actually do need some hardening on the server it should not just silently do nothing, it should return an error response or close the connection or both
Kiel
KielOP13mo ago
I'm not the one who wrote the server, so I guess I can forward your complaints along lol
JakenVeina
JakenVeina13mo ago
ah well, that's significant info
Want results from more Discord servers?
Add your server