如何为 Azure SignalR Serverless 配置 KeepAliveInterval HubOptions?

问题描述 投票:0回答:1

使用 Azure SignalR 服务时,是否可以为 SignalR 集线器配置 KeepAliveInterval 选项?

我主要是一名前端开发人员,所以我对后端设置不是很熟悉。据我了解,我们在进程内模型中使用 Azure SignalR 服务和 ServerlessHub。您可以查看相关文档

我已经浏览了可用的文档,但尚未找到配置此选项的方法。一位后端开发人员提到应该将其添加到我们的

local.settings.json
中。目前,我们的配置如下所示:

{
  "values": {
     "Azure:SignalR:HubProtocol:NewtonsoftJson:CamelCase": true,
   }
}
azure azure-functions signalr signalr-hub azure-signalr
1个回答
0
投票

配置

KeepAliveInterval
以使用
AddSignalR
文件中的
Startup.cs
方法设置服务器使用的时间间隔,以配置 SignalR 服务选项

KeepAliveProperty
中设置
Startup.cs

options.KeepAliveInterval = TimeSpan.FromSeconds(120);

我创建了一个 .NET 进程内 Signal Azure 函数,并使用下面的代码在

KeepAliveInterval
中配置
Startup.cs

Startup.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Azure.SignalR;
using System;
using System.Threading.Tasks;

[assembly: FunctionsStartup(typeof(FunctionApp35.Startup))]

namespace FunctionApp35
{ 
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSignalR(options =>
                {
                    // Set the KeepAliveInterval
                    options.KeepAliveInterval = TimeSpan.FromSeconds(120);
                });
        }
    }
}

函数.cs:

   [FunctionName("negotiate")]
   public static SignalRConnectionInfo Negotiate(
       [HttpTrigger(AuthorizationLevel.Anonymous, "get","post")] HttpRequest req,
       [SignalRConnectionInfo(HubName = "eventhub")] SignalRConnectionInfo connectionInfo)
   {
       return connectionInfo;
   }

控制台响应:

Functions:

        negotiate: [GET,POST] http://localhost:7003/api/negotiate

For detailed output, run func with --verbose flag.
[2024-09-17T12:46:35.657Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-09-17T12:46:42.135Z] Executing 'negotiate' (Reason='This function was programmatically called via the host APIs.', Id=f23d5ee9-8XXX9bd-a108-005c4d235fa3)
[2024-09-17T12:46:42.205Z] Executed 'negotiate' (Succeeded, Id=f23d5eeXXX8-49bd-a108-005c4d235fa3, Duration=245ms)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.