[ClientWebSocket] Immediate closure after sending/receiving message #97033
-
Hello! I have to use ClientWebSocket on my project to connect to specific TCP server over WebSocket, i do connect successfully, but then i get immediately close event, no matter what i do (sending command, or receiving anything). I checked WSS with postman/online websocket and connection working fine, messages are transmitted. Specs: Example using (var client = new ClientWebSocket())
{
await client.ConnectAsync(access.AccessUrl, CancellationTokenSource.Token);
Console.WriteLine("Connected to websocket!");
var buffer = new byte[1024];
var segment = new ArraySegment<byte>(buffer);
var sent = false;
while (client.State == WebSocketState.Open)
{
if (!sent)
{
await client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("{\"user_agent\":\"ethereal-faf-client\",\"version\":\"2.2.0\",\"command\":\"ask_session\"}\r\n")), WebSocketMessageType.Binary, false, CancellationTokenSource.Token);
sent = true;
}
else
{
var result = await client.ReceiveAsync(segment, CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Text)
{
var message = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
Console.WriteLine($"Received message: {message}");
}
sent = false;
}
await Task.Delay(1000);
}
Console.WriteLine("Disconnected from websocket.");
} Exception System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('CloseReceived') for this operation. Valid states are: 'Open, CloseSent'
at System.Net.WebSockets.WebSocketValidate.ThrowIfInvalidState(WebSocketState currentState, Boolean isDisposed, WebSocketState[] validStates)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsync(ArraySegment`1 buffer, CancellationToken cancellationToken)
--- End of stack trace from previous location ---
at Ethereal.FAF.UI.Client.ViewModels.LobbyConnectionViewModel.<OnConnectCommand>b__14_0()
at AsyncAwaitBestPractices.SafeFireAndForgetExtensions.HandleSafeFireAndForget[TException](Task task, Boolean continueOnCapturedContext, Action`1 onException)
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) Logs from another library (https://github.com/Marfusios/websocket-client)
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Websocket bridge is setup behind cloudfare and short lived, if someone need a link, please ping me. Last one generated wss://ws.faforever.com/?verify=1705415491-GSTR6YzqX2d5R4T1uMx8lJ7PuF90oa4fBA9XaRwUON0%3D |
Beta Was this translation helpful? Give feedback.
-
The problem was incorrect linebreak that was passed to channel |
Beta Was this translation helpful? Give feedback.
The problem was incorrect linebreak that was passed to channel