Issue
In my project, I need to reconnect to a SignalR Web Socket if the internet connection is lost.
I'm working with Angular Ionic V4, and I installed the Network Information plugin.
On the plugin's "Connected" event - I'm trying to call this._hubConnection.start()
with the original token. It appears a new connection is established, but every time I get data from the hub, the event runs twice ( or X times depending how many re-connections I've made).
I beleive what's happening is the SignalR creates another socket under the same key, and when a message is sent to the specified key - it is broadcasted to all connections with same key hence duplicates the events.
My question is - Is there a way to "reconnect" to an existing socket instead of "making an additional connection"?
EDIT:
After trying to upgrade to @microsoft/signalr, this is what I got:
It appears it tries to reconnect while the network is offline, and when the network is back it connects and disconnects.
I tried debugging my SignalR server. This is the method that's invoking the events on the client:
public async Task Enqueue(int processId)
{
var processEnqueueAppointmentResults = await _processesService.Enqueue<object>(processId);
await Clients.User($"PROCESS_{processId}").SendAsync("ProcessEnqueued", processEnqueueAppointmentResults);
}
The debugger entered Enqueue once, and on the client - I received 4 duplicate logs (meaning the event was fired 4 times)
Solution
Solved
The problem was that on my Internet connected event I called my createConnection()
method which initializes the SignalR connection
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl(`${this.env.testUrl}/hubs/patients`, {
accessTokenFactory: () => token
})
.build();
Instead, just had to invoke the this._hubConnection.start();
method on my existing hubConnection which solved the duplicate invocation (Just as I thought, every reconnect event added another socket under the same key. When SignalR wanted to invoke the event on the client - it fired the number of times I invoked createConnection()
)
Answered By - Yoni Ziv
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.