question

vsipranav avatar image
0 Votes"
vsipranav asked ryanchill commented

Always getting the error parameter as 'undefined' in an onclose event of the SignalR Typescript client.

Hello,
I am trying to implement simple chat application using SignalR. For front-end, I am using Angular 10 with Typescript SignalR client. For this client I am using the latest @microsoft/signalr package. While the back-end server is in .Net Core 3.1 WebAPI project. Also, I am using the Azure SignalR service with Standard pricing tier.
I am referring the following documentation.
https://docs.microsoft.com/en-us/javascript/api/@microsoft/signalr/hubconnection?view=signalr-js-latest#onclose--error---error-----void-
As per the documentation, the 'onclose' event receives an optional error parameter if there is any. But in my case in the client code, I am always getting this error as 'undefined'. I want to use this error parameter to determine if the connection was closed purposefully by client or was there any error either from client or server and based on that I want to take the further action in code. Find below my code snippet.

 this.hubConnection.onclose((error) => {
   // Try to log the error.
   if (typeof error !== 'undefined') {
     console.log(this.TAG + '(onclose) Error: ' + error.name + ' : ' + error.message);
   } else {
     console.log(this.TAG + '(onclose) No error to show while closing the connection.');
   }

   // Log the connection state.
   console.log(this.TAG + '(onclose) HubConnection State: ' + this.hubConnection.state);
 });

Kindly please take a look at this and let me know if I am doing it correctly or is there any other way to do this. Also, if you need more details about the issue, please let me know.

Thanks.

azure-signalr-servicedotnet-aspnetcore-realtime
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Update:

In .Net Client, the error received in onclose event when wifi internet connection is closed is as follows.

Closed Event Error: Reconnect retries have been exhausted after 5 failed attempts and 00:01:15.6488667 elapsed. Disconnecting.

Code Snippet:

     connection.Closed += async (error) =>
     {
         Dispatcher.Invoke(new Action(() =>
         {
             if (error != null)
             {
                 Debug.WriteLine($"Closed Event Error: {error.Message}");
             }
         }));
         await connection.DisposeAsync();
         await Task.CompletedTask;
     };
0 Votes 0 ·

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered ryanchill commented

Hi @vsipranav-0854,

As you stated, error is an optional parameter for onclose and is only populated when connection is closed due to an actual error. The code snippet you provided seems perfectly fine to me.

Regards,
Ryan


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ryanchill Thanks for the reply.
Okay, but then how can I get to know when the error parameter is populated with some error? Are there any specific events?
In chatting application, sometimes user gets disconnected due to network issues. But the problem is even if the disconnection happens on purpose or by network issue, it ultimately gets into the 'onclose' event in both cases. And I am not able to determine the cause of disconnection.
I developed one sample .Net client for testing and to simulate the network issue on my system, I started the application and after successful connection, I turned off the WIFI for some time. But unlike the Typescript client, I am receiving the following error.

Reconnect retries have been exhausted after 5 failed attempts and 00:01:15.6488667 elapsed. Disconnecting.

I am not able to understand, why am I getting different behaviour for different clients. Kindly please take a look at the comment above for .Net code sample.

Thanks!

0 Votes 0 ·

@vsipranav-0854, the error object should be populated when the connection between client and SignalR service is disconnected due to an actual error. If you're not seeing this expected behavior, it's best to file a new bug. I did see a couple of open bugs, https://github.com/dotnet/aspnetcore/issues?q=is%3Aissue+is%3Aopen+onclose, but none related to your exact issue.

Regards,
Ryan


1 Vote 1 ·