Azure / azure-signalr

Azure SignalR Service SDK for .NET
https://aka.ms/signalr-service
MIT License
427 stars 101 forks source link

When OnDisconnect is invoked, there is no Exception passed through. #1421

Open ZhiqingWan opened 3 years ago

ZhiqingWan commented 3 years ago

Describe the bug

When that OnDisconnect method is called, it appears as though there is no Exception passed through, so there's no way to differentiate between a user that has just closed their browser, or a user that has been experiencing connectivity issues. SignalR for .NET Framework would pass through a boolean informing the server that it was as a result of a timeout.

To Reproduce

Exceptions (if any)

Further technical details

terencefan commented 3 years ago

I'll see if SignalR protocol could support this.

ajbeaven commented 3 years ago

How'd you get on with this @terencefan?

My use case is a chat app. If two users are chatting and one closes the browser, the OnDisconnectAsync method is called and the chat is marked as stopped. OnDisconnectAsync is also fired if a user is experiencing connectivity issues - in those cases I don't want to mark the chat as stopped as the client will attempt to reconnect and the chat can be continued if that is successful.

At present, because this Exception is not passed through (despite the docs saying it should be) there is no easy way to handle these two cases separately. IIRC, the exception was passed through correctly in SignalR for .NET Framework using ASRS but maybe I was mistaken; I've just migrated from that version.

ajbeaven commented 3 years ago

Any further updates on this one?

ajbeaven commented 2 years ago

Has someone been able to look in to whether this can be supported on ASRS? If not, I really need some direction on how to differentiate on the server side between a disconnection that occurs due to the client closing their browser vs a client losing their internet connection. @vicancy, is this something you could help with?

vicancy commented 2 years ago

Sorry for the late response.

differentiate client disconnection due to closing their browser vs a client losing their internet connection

When client "loses internet connection", SignalR hub leverages the SignalR client ping messages to determine if the client is still there, however SignalR swallows the OperationCancelledException https://github.com/dotnet/aspnetcore/blob/88ee825bca9978df7f1d8c8c09b71b6dffeeb53b/src/SignalR/server/Core/src/HubConnectionHandler.cs#L169 and as a result it fails to differentiate from these 2 conditions and here is the issue https://github.com/dotnet/aspnetcore/issues/26701

In such a scenario, the Azure SignalR acts as a proxy and there is little Azure SignalR can do to change the behavior in SignalR application layer.