0

I have an Angular client which uses signalR library "@aspnet/signalr": "^1.1.4"

I need it just to receive notifications from ASP.net server. So I build and start hubConnection which is private hubConnection: signalR.HubConnection; and then subscribe on different messages from server

this.hubConnection.on("SomeMessage", (data: MyDto) =>
  myActions()
);

All works fine and receives messages but after several hours of work something happens (with network I suppose) and on server fires method

public override async Task OnDisconnectedAsync(Exception e)

That is fine, but I want to handle disconnections on client's side. I read this and this and many more answers but my hubConnection has neiter disconnect method nor hub property which are mentioned in those answers. So I would appreciate any hints where they could be. Preferable in typescript, not in html.

UPDATED

Well, as was proposed I changed signalR package to "@microsoft/signalr": "^5.0.4" by I still can't find onDisconnect event

There is an instance of signalR.HubConnection which I build as shown below

  private buildConnection(url: string, token: string) {
      this.hubConnection = new signalR.HubConnectionBuilder()
      .withUrl(url, { accessTokenFactory: () => token })
      .build();
  }

But this instance does not have onDisconnect event.

mli
  • 420
  • 1
  • 4
  • 10

1 Answers1

0

As was commented, upgrade to the newer version, which now has an event for OnDisconnect and a way to access the reason in the client.

SignalR 2.1 adds an overload to the server OnDisconnect event that indicates if the client deliberately disconnected rather than timing out. The StopCalled parameter is true if the client explicitly closed the connection. In JavaScript, if a server error led the client to disconnect, the error information will be passed to the client as $.connection.hub.lastError.

$.connection.hub.disconnected(function () {
    if ($.connection.hub.lastError) 
        { alert("Disconnected. Reason: " +  $.connection.hub.lastError.message); }
});

Reference Documentation - https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/handling-connection-lifetime-events#detecting-the-reason-for-a-disconnection

Frank M
  • 1,379
  • 9
  • 18