首页 > 解决方案 > SignalR 连接在 20 分钟后中断

问题描述

我在 .NET Core 3.1 下使用 Kestrel 将 .NET Core SignalR Hub 作为 Windows 服务运行。它由具有最新客户端库 (v. 5.0.9) 的 Javascript SignalR 客户端调用

可重现,20分钟后连接无故停止,2秒后重新连接。但是,不会在新连接上发送新数据。

我没有更改任何默认超时设置。

错误日志 Javascript 控制台

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddSignalR(hubOptions =>
            {
                hubOptions.EnableDetailedErrors = true;
                hubOptions.MaximumReceiveMessageSize = 1048576; //1MB

            }).AddJsonProtocol(options =>
            {
                options.PayloadSerializerOptions = ServiceTools.GetSerializerOptions();
                options.PayloadSerializerOptions.PropertyNameCaseInsensitive = true;
            });

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCors(builder => builder
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(_ => true)
                .AllowCredentials()
            );

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<VisuNotificationHub>("/signalr");
            });

          
        }
    }
pfw2.connection = new signalR.HubConnectionBuilder()
            .withUrl(signalrUrl,  ['webSockets', 'serverSentEvents', 'longPolling'])
            .configureLogging(signalR.LogLevel.Debug)
            .withAutomaticReconnect({
                nextRetryDelayInMilliseconds: retryContext => {
                    if (retryContext.elapsedMilliseconds < 10000) {             //<10sec
                        return 1000;                                            //sekündlich
                    }
                    else if (retryContext.elapsedMilliseconds < 60000) {        //<60sec/1min
                        return 10000;                                           //10-sekündlich
                    }
                    else if (retryContext.elapsedMilliseconds < 1800000) {      //<30min
                        return 300000;                                          //5-minütlich
                    }
                    else {                                                      //>= 30min
                        return 900000;                                          //15-minütlich
                    }
                }
            })
            .build();

        pfw2.connection.serverTimeoutInMilliseconds = 60000;

        //Reconnect Handler
        pfw2.connection.onreconnecting(error => {
            
            var reconnectErrorData = [{ Message: `<p style="background:red;color:white;font-size:24px">@ResOperatorDialog.ErrorPleaseRefresh</p>` }];
            console.assert(connection.state === signalR.HubConnectionState.Reconnecting);
            console.log("Error in Hub!", error);
            UpdateTicker(reconnectErrorData);
        });

        pfw2.connection.onreconnected((connectionId) => {
            onHubConnectionDone();
            console.log(connectionId);
        });

        //Closed handler
        pfw2.connection.onclose((e) => {
            var connectioNClosedErrorData = [{Message: `<p style=\"background:red;color:white\">Connection closed". Try refreshing this page to restart the connection.</p>`}];
            UpdateTicker(connectioNClosedErrorData);
            console.log(e);
        });   
       
        //connection Start
        pfw2.connection.start()
        .catch((err) => {
            return console.error(err.toString());
        })
        .then(() => {
            console.log("connected, ID=",pfw2.connection.connectionId);
            onHubConnectionDone();
        });

标签: javascript.net-coreasp.net-core-signalr

解决方案


推荐阅读