javascript - ASP.NET Core 3.0 MVC 连接中的 SignalR 保持事件状态不起作用?

标签 javascript c# typescript asp.net-core signalr

一句话中的问题:SignalR 连接是否通过 keep-alives 保持打开状态,如果是,为什么它们不能在 .NET Core 3 中工作,如果不是,那么 keep-alive 设置的用途是什么?

我有一个 ASP.NET Core 3.0 MVC Web 应用程序,我刚刚按照此处的入门教程添加了 SignalR:https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.0&tabs=visual-studio (减去创建一个新的网络应用程序,我们只是将其添加到我们的应用程序中)

我有一个简单的集线器,客户端连接成功,如果我在建立连接后立即测试 SignalR 方法,它就可以工作。但是,如果我 30 秒不使用它,连接就会关闭。如果我理解文档,保活的默认值是 15 秒,但我没有看到任何保活消息。我尝试了 KeepAliveInterval 和 ClientTimeoutInterval 的各种设置,但没有解决这个问题。

我将 .withAutomaticReconnect() 添加到 JavaScript 中的 HubConnectionBuilder 调用中,这确实可以在每 30 秒断开连接后重新建立连接。这是应该如何工作的,还是应该通过 ping 保持连接,并且只需要由于网络丢失/等原因重新连接?我觉得我错过了一些简单的东西,或者我误解了它应该如何工作。

以下是我们代码的各个部分:

Startup.csConfigureServices方法:

        services.AddSignalR(hubOptions =>
        {
            hubOptions.EnableDetailedErrors = true;
            //hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(10);
            //hubOptions.ClientTimeoutInterval = TimeSpan.FromMinutes(1);
        });

Startup.cs配置方法:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
            endpoints.MapHub<QuoteHub>("/quoteHub");
        });

我们的报价中心:

public class QuoteHub : Hub
{
    public QuoteHub()
    {

    }

    public override Task OnConnectedAsync()
    {
        var quoteId = Context.GetHttpContext().Request.Query["quoteId"];

        return Groups.AddToGroupAsync(Context.ConnectionId, quoteId);
    }
}

以及连接的 javascript 设置:

const setupQuoteConnection = (quoteId) => {
    let connection = new signalR.HubConnectionBuilder()
    .withUrl("/quoteHub?quoteId=" + quoteId)
    .configureLogging(signalR.LogLevel.Debug)
    .withAutomaticReconnect()
    .build();

    connection.on("ReceiveUpdate", (update) => {
    alert(update);
    }
    );

    connection.start()
    .catch(err => console.error(err.toString()));
};

为了彻底起见,调用中心将更新发送给客户端:

_quoteHub.Clients.Group(domainEvent.QuoteId.ToString()).SendAsync("ReceiveUpdate", domainEvent.TotalPrice);

更新

我发现聊天样本位于 https://github.com/aspnet/SignalR-samples 。我下载并运行了该示例,它工作正常,连接保持打开状态,但我一生都看不出是什么导致它的行为与我的应用程序不同。

我确实注意到我的中心存在一些问题并修复了它,尽管这没有什么区别:

public class QuoteHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        var quoteId = Context.GetHttpContext().Request.Query["quoteId"];

        await Groups.AddToGroupAsync(Context.ConnectionId, quoteId);

        await base.OnConnectedAsync();
    }
}

然后我更新了 JavaScript 代码来配置连接并延长服务器超时。这确实有效,但是当上面链接的聊天示例没有它并且没有它也可以正常工作时,为什么我需要它?

    let connection = new signalR.HubConnectionBuilder()
    .withUrl("/quoteHub?quoteId=" + quoteId)
    .configureLogging(signalR.LogLevel.Debug)
    .withAutomaticReconnect()
    .build();

    connection.serverTimeoutInMilliseconds = 3600000; //1 hour

最佳答案

我想你可能还需要添加选项。像这样的传输

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR(hubOptions =>
    {
        hubOptions.EnableDetailedErrors = true;
        hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<MyHub>("/myhub", options =>
        {
            options.Transports = HttpTransportType.LongPolling; // you may also need this
        });
    });
}

关于javascript - ASP.NET Core 3.0 MVC 连接中的 SignalR 保持事件状态不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58756908/

相关文章:

javascript - Vue.js - 推送到对象而不跟踪

JavaScript 查找 : update value of object in array if object key exists in another object array similar to the v-lookup in excel

interface - Typescript-使用类作为接口(interface),为什么我必须实现私有(private)成员/方法?

php - 将 PHP 变量传递给 Javascript window.location

javascript - 更改聚合结果

c# - 解析是获取成员类型的唯一方法吗?

c# - 在 C# 中初始化一个 Generic.List

c# - 类名的 CompilerServices

HTTP 和嵌套对象 (Angular2 RC1 + TS)

postgresql - TypeOrm 不会在更新时保存实体及其关系