signalr js 客户端 onReconnecting() 未触发

signalr js client onReconnecting() not fired

我在处理 signalR js 客户端上的连接生命周期事件时遇到问题。我在客户端使用 aspnet/signalr-client npm 库,在 asp.net 核心服务器上使用 microsoft.aspnetcore.signalr 库。

问题是

当我关闭服务器 (alt+F4) 时,客户端方法 onClosed(error) 被触发,我想要的是 documentation 中的描述方式 onReconnecting()方法被触发,客户端尝试重新连接。我已经尝试忽略此问题并在 onClosed(error) 方法中建立新连接,但随后出现 Uncaught (in promise) Error: Cannot start a connection that is not in the 'Initial' state. 错误。

我的问题是

为什么 onClosed(error) 方法被触发并且客户端不尝试重新连接,我该如何解决这个问题?

澄清代码:

客户

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();

服务器中心 'TestHub'

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();
        }
    }
}

Asp.NET 核心的 SignalR 没有重新连接。您在先前版本的 SignalR 文档中查找了 onReconnecting,但该功能在新版本中不再存在。查看描述两者之间更大突破性变化的 announcement