I am getting trouble handling connectivity lifetime events on my signalR js client. I am using the aspnet/signalr-client npm library on the client and the microsoft.aspnetcore.signalr library on the asp.net core server.
when I close the server (alt+F4) the client method onClosed(error)
gets fired, what I want is, how it is described in the documentation, that the onReconnecting()
method gets fired and the client tries to reconnect. I have already tried to ignore this problem and establish a new connection in the onClosed(error)
method, but then i get a Uncaught (in promise) Error: Cannot start a connection that is not in the 'Initial' state.
Error.
Why does the onClosed(error)
method get fired and the client doesn't try to reconnect and how could I probably fix this?
var connection = new signalR.HubConnection('http://localhost:8956/testhub');
connection.on('onStart', function() {
console.log('onStart');
});
connection.on('onEnd', function() {
console.log('onEnd');
})
console.log('connection=', connection);
connection.onClosed = function(error) { // this gets fired
console.log('connection closed');
//setTimeout(function() {
// connection.start();
//}, 10000);
}
connection.onReconnecting = function() { // this not
console.log('trying to reconnect');
}
connection.start();
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace SignalRCoreConsole.Hubs
{
class TestHub : Hub
{
public void Start()
{
Clients.All.InvokeAsync("onStart");
Console.WriteLine("TestHub: Start");
}
public void End()
{
Clients.All.InvokeAsync("onEnd");
Console.WriteLine("TestHub: End");
}
public override Task OnDisconnectedAsync(Exception exception)
{
Console.WriteLine("client disconnected");
return base.OnDisconnectedAsync(exception);
}
public override Task OnConnectedAsync()
{
Console.WriteLine("client connected");
return base.OnConnectedAsync();
}
}
}
SignalR for Asp.NET Core does not have reconnects. You looked up the onReconnecting
in the documentation for the previous version of SignalR but the feature is no longer there in the new version. Take a look at the announcement that describes bigger breaking changes between the two.