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

html client on page refresh or tab close websocket disconnect , is not fired on server side during message process #56

Open
ManoLazar opened this issue Sep 4, 2020 · 4 comments

Comments

@ManoLazar
Copy link

ManoLazar commented Sep 4, 2020

...
while(webSocket.IsConnected && !cancellation.IsCancellationRequested) {
try {
var messageText = await webSocket.ReadStringAsync(cancellation).ConfigureAwait(false);
...
messageProcess(messageText) ;
await webSocket.WriteStringAsync(messageText, cancellation).ConfigureAwait(false);
...
so:
during the messageProcess () - can be with await
when html client reload page or TAB close, the webSocket is not fired as disconnect ??
and cause stuck on webSocket.WriteString...

if it is not during the messageProcess the server websocket listner fires correctly

why the code is written with while(webSocket.isConnected...) ?

and not just :

websocket.OnMessage(...)

webSocket.OnDisconnect(...)

webSocket.OnError(...)

so even on during messageProcess, if occur the disconnect event it will be fired ?

  1. can you send any code example how to resolve the messageProcess ?

any help ?

regards,
Mano

@deniszykov
Copy link
Owner

Hi @ManoLazar! WebSocket instance doesn't have control flow, so it can't emit events or even update it's internal state (IsConnected, etc) till ReadMessageAsync is called.
So it is mandatory to call ReadMessageAsync in loop, and check if read message is not null (closing message). If read message is null, when you need to close WebSocket immediately with CloseAsync().

@ManoLazar
Copy link
Author

ManoLazar commented Sep 7, 2020

what about the 1st topic , detect from client to server websocket changes during
messageProcess(messageText) ;?

@deniszykov
Copy link
Owner

deniszykov commented Sep 7, 2020

Actually I can't reproduce.
Writing into disconnected socket leads to:
a) error
b) success
not hung.

Also it's reporting disconnect on next ReadMessageAsync by returning null message.

Can you find right conditions to reproduce this behavior?

@ManoLazar
Copy link
Author

ManoLazar commented Oct 9, 2020

Hi @deniszykov
1.
at last I found somesthing, that work for me, so:
I run the messageProcess in a task :
Task.Run(() => messageProcess(messageText) ( in the HandleConnectionAsync...)
but without the await (without the await )
now during the messageProcess(that can take 1 to 30 seconds)
when on client side I reload a page(means re-browse) or close the browser TAB,
I got immediate response on the attached websocket that is disconnected, and got the correct status .
on Ending the message process I can recognize the correct webSocket link of the MessageProcces,
and detect that it is disconnected (or become null)

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