I’ve been trying to figure this out for two entire months. I’ve had this code over here (SendCommand) in C# backend
{
var id = Guid.NewGuid().ToString();
var cmd = new RenderRequest()
{
command = command,
args = arguments,
id = id,
};
var res = new TaskCompletionSource<RenderResponse<Stream>>();
var responseMutex = new Mutex();
// Setup listener
mux.WaitOne();
resultListeners[id] = stream =>
{
Console.WriteLine("[info] SendCommand() over");
lock (responseMutex)
{
res.SetResult(stream);
}
return 0;
};
mux.ReleaseMutex();
// Send message
var bits = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(cmd));
while (ws is not {State: WebSocketState.Open})
{
Writer.Info(LogGroup.GeneralRender, "Ws not available, retry in a second");
#if DEBUG
await Task.Delay(TimeSpan.FromSeconds(60), cancellationToken ?? CancellationToken.None);
#else
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken ?? CancellationToken.None);
#endif
if (cancellationToken is {IsCancellationRequested: true})
throw new TaskCanceledException();
}
await ws.SendAsync(bits, WebSocketMessageType.Text, true, cancellationToken ?? CancellationToken.None);
await using var register = cancellationToken?.Register(() =>
{
mux.WaitOne();
resultListeners.Remove(id);
mux.ReleaseMutex();
// TODO: Would be nice if we could send a message to WS telling it to cancel the task
// TrySetCanceled instead of SetCanceled due to WEB-35
lock (responseMutex)
{
if (res.TrySetCanceled(cancellationToken.Value) && command != "Cancel")
{
SendCommand("Cancel", new List<dynamic>()
{
id,
}, CancellationToken.None);
}
}
});
var resp = await res.Task;
return resp;
}
and Here for ConnectionManager
{
while (true)
{
try
{
mux.WaitOne();
ws ??= new ClientWebSocket();
var wsCurrentState = ws.State;
mux.ReleaseMutex();
if ((wsCurrentState is WebSocketState.Aborted) || (wsCurrentState is WebSocketState.Closed) || (wsCurrentState is WebSocketState.None) || (wsCurrentState is WebSocketState.CloseReceived) || (wsCurrentState is WebSocketState.CloseSent))
{
Console.WriteLine("[info] ws connection is in state {0}, so we are re-connecting", ws.State);
mux.WaitOne();
ws = new ClientWebSocket();
mux.ReleaseMutex();
await ws.ConnectAsync(wsUrl, CancellationToken.None);
}
await ListenForMessages();
}
catch (Exception e)
{
Console.WriteLine("[info] ConnectionManager got error in ws connection {0}", e.Message);
await Task.Delay(TimeSpan.FromSeconds(5));
}
}
}
Anyone know why the websocket is always in the state closed?
Tried removing the check, everything, tried so many things, everyone who has the fix is gatekeeping it from me. I would love this to god if I can ever get this to work. It’s crazy how stupid this is getting to be. I thought this would be easy but this has turned out to be the most annoying thing I’ve ever dealt with in my entire life. It’s getting so crazy. If anyone can help, I’d do anything at this point due to how stupid this is really getting on me. Hopefully one of you guys can find the problem here and tell me what I’m doing wrong, or how to open up the websocket. That’s all I need. Thanks!