1

I am trying to handle disconnect error with reconnection so I used the nextRetryDelayInMilliseconds method for reconnection, but in this case, if the connection is lost or there are some problems with the connection, the onclose handler does not work.

I am using "@microsoft/signalr": "^7.0.2" package in Angular project

I used "withAutomaticReconnect" option, but it doesn't fire immediately. I get a callback after some timeout.

Connection builder:

this.hubconnection = new HubConnectionBuilder()
  .withUrl(url)
  .withAutomaticReconnect({
    nextRetryDelayInMilliseconds: () => {
      this._errorState$.next(true);
      return 1000;
    }
  })
  .build();

Error handler (which doesn't fire if I use "withAutomaticReconnect"):

this.hubConnection.onclose(error => callback(error)

Expecting to handle the error immediately when there are some errors related to connection.

pantonis
  • 5,601
  • 12
  • 58
  • 115

1 Answers1

0

After test, I found the correct format should be like below.

connection.onclose(() => {
    
});

In your scenario, code should be

this.hubConnection.onclose((error) => {
    console.error(`Something went wrong: ${error}`);
});

After testing, if you restart the signalr server, you may not get the error information, which should be normal. I don’t know if you can see more in the production environment. This is the standard usage, you can also refer to the following Link.

onclose event in signalr client


Suggestion:

  1. The test above I test in signalr javascript client, and I also reproduce the issue, if it not works, please use @microsoft/signalr 7.0.0 version to troubleshoot problems caused by the new version.

  2. I also using reconnectDelay property when build the connection.

    var connection = new signalR.HubConnectionBuilder().withUrl("/mainHub")
     .withAutomaticReconnect({
         nextRetryDelayInMilliseconds: () => {
             this._errorState$.next(true);
             return 1000;
         },
         reconnectDelay: 500 // set the reconnect delay to 500ms
     })
     .configureLogging(signalR.LogLevel.Trace).build();
    
Jason Pan
  • 15,263
  • 1
  • 14
  • 29