Connection immediately closes after success connection #141
-
|
Hi! I have to use websocket client to connect to specific server to make communication. Every attempt of connecting was successful, i was getting upgrade response, but then my client would receive immediately close event (1005). I`ve tried a few libraries so far none of them worked. I checked wss url with postman/online websocket tests, and everywhere else connection working fine, messages are transmitted. Connection using default ClientWebSocket, it stops right at SendAsync() with exception, that state CloseReceived. using (var client = new ClientWebSocket())
{
await client.ConnectAsync(access.AccessUrl, CancellationTokenSource.Token);
await client.SendAsync(Encoding.UTF8.GetBytes("{\"user_agent\":\"ethereal-faf-client\",\"version\":\"2.2.0\",\"command\":\"ask_session\"}"), WebSocketMessageType.Text, WebSocketMessageFlags.None, CancellationTokenSource.Token);
while (!CancellationTokenSource.IsCancellationRequested)
{
var bytes = new byte[1024];
var result = await client.ReceiveAsync(bytes, CancellationTokenSource.Token);
string res = Encoding.UTF8.GetString(bytes, 0, result.Count);
}
CancellationTokenSource = null;
}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)Example with current library i just receive close event using (var client = new WebsocketClient(access.AccessUrl, _logger))
{
client.ReconnectTimeout = TimeSpan.FromSeconds(30);
client.MessageReceived.Subscribe(msg => Console.WriteLine(msg.Text));
await client.StartOrFail();
client.IsReconnectionEnabled = false;
client.DisconnectionHappened.Subscribe(async x =>
{
if (CancellationTokenSource == null) return;
var lobbyAccess = await _fafUserApi.GetLobbyAccess(CancellationTokenSource.Token);
client.Url = lobbyAccess.AccessUrl;
_logger.LogInformation(lobbyAccess.AccessUrl.ToString());
await client.Reconnect();
});
Task.Run(() => client.Send("{\"user_agent\":\"ethereal-faf-client\",\"version\":\"2.2.0\",\"command\":\"ask_session\"}\r\n"))
.SafeFireAndForget();
while (!CancellationTokenSource.IsCancellationRequested)
{
await Task.Delay(1000);
}
CancellationTokenSource = null;
}```
Logs
```log
info: Websocket.Client.WebsocketClient[0]
wss://ws.faforever.com/?verify=1705339993-QD6XeFLHqB6OAUom8%2F6ji9XKUz0KfkypYcBeQX%2BEUTc%3D
dbug: Websocket.Client.WebsocketClient[0]
[WEBSOCKET (null)] Starting..
trce: Websocket.Client.WebsocketClient[0]
[WEBSOCKET (null)] Received close message
info: Websocket.Client.WebsocketClient[0]
[WEBSOCKET (null)] Client is already stopped
dbug: Websocket.Client.WebsocketClient[0]
[WEBSOCKET (null)] Client is not connected to server, cannot send: {"user_agent":"ethereal-faf-client","version":"2.2.0","command":"ask_session"} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hey, |
Beta Was this translation helpful? Give feedback.
Hi!
Problem was on my end, after successful authorization i sent broken command and server killed my connection.
Thanks