WebSocketListener icon indicating copy to clipboard operation
WebSocketListener copied to clipboard

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

Open ManoLazar opened this issue 5 years ago • 4 comments

... 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

ManoLazar avatar Sep 04 '20 09:09 ManoLazar

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().

deniszykov avatar Sep 07 '20 09:09 deniszykov

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

ManoLazar avatar Sep 07 '20 09:09 ManoLazar

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?

deniszykov avatar Sep 07 '20 10:09 deniszykov

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)

ManoLazar avatar Oct 09 '20 14:10 ManoLazar