Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is OnClose always triggered? #1

Open
bilogic opened this issue Nov 7, 2024 · 3 comments
Open

Is OnClose always triggered? #1

bilogic opened this issue Nov 7, 2024 · 3 comments

Comments

@bilogic
Copy link

bilogic commented Nov 7, 2024

CancellationTokenSource.Cancel();

After the server gives me a response for logging off, I attempt to close the connection with this repo's ws.Close(CoolWebSocketCloseStatus.NormalClosure), however OnClose was not triggered.

If I send ws.Close(4) (random integer), I get a unhandled OperationCanceledException, "the operation was cancelled" instead.

Any idea how and when OnClose was designed to trigger?

@bilogic bilogic changed the title Is cancellation required? Is OnClose always triggered? Nov 7, 2024
@realcoloride
Copy link
Owner

Hello there, this is strange. OnClose should be triggered whenever a fatal issue is encountered leading to the websocket being disconnected or when the websocket is disconnected manually/from server.

Perhaps the token is already cancelled (or the internal websocket disconnected) beforehand?

Could you try checking the Websocket's IsOpen variable before calling ws.Close in your case? And could you check if any errors are emitted with the OnError event?

I have never seen this happen before, but my guess is a premature internal websocket client disconnection (causing the token to be cancelled before).

Cheers

@bilogic
Copy link
Author

bilogic commented Nov 7, 2024

Hi thanks for getting back.

My app is targetted at 4.6.2, so I had to make syntax changes to CoolWebSocketClient.cs and add a program.cs at this commit 9b76343

It is probably easiest to compile from my repo and see if it behaves according to design

  1. I added the event handlers according to your README
  2. I see Connected to server from OnOpen, to me, that means event was fired, means syntax is ok
  3. However, after calling ws.Close(), ws.OnClose() never fires

My assess is some logic issue in the original CoolWebSocketClient.cs, my expectation is OnClose should fire once the websocket is disconnected, whether there is an exception or not.

@realcoloride
Copy link
Owner

Hello.

I'm not very sure to understand the changes relevant to fixing the problem. If the issue roams from OnClose never being fired, I believe I might see where this might be from.

await WebSocket.CloseAsync((WebSocketCloseStatus)closeStatus, closeMessage, CancellationToken);

I do not call OnClose in that case I believe. I pushed the following (c9c57fe) commit to add the following line:

OnClose?.Invoke(closeStatus, closeMessage);

I will do some QA and testing soon, but feel free to test on your case with the following commit if this fixes your issue or not.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants