简体   繁体   English

带有 ASP.Net Core 的 Azure SignalR 无服务器

[英]Azure SignalR serverless with ASP.Net Core

I need to connect ASP.Net Core with SignalR in serverless mode.我需要在无服务器模式下将 ASP.Net Core 与 SignalR 连接起来。 I couldn't find any good example of how to do it (only for Default mode) and if it's even possible?我找不到任何关于如何做到这一点的好例子(仅适用于默认模式),如果它甚至可能?

We do have Azure functions, that work fine with serverless SignalR.我们确实有 Azure 功能,可以很好地与无服务器 SignalR 配合使用。 But now we need to connect to another web server, that is running ASP.Net Core.但现在我们需要连接到另一个运行 ASP.Net Core 的 Web 服务器。 How can we do it?我们该怎么做? So that Azure function and ASP.Net Core server share the same SignalR Service?那么 Azure 函数和 ASP.Net Core 服务器共享同一个 SignalR 服务?

Here is an example of connecting to SignalR (Replace 'localhost' with your Azure SignalR address)这是连接到 SignalR 的示例(将“localhost”替换为您的 Azure SignalR 地址)

class Program
{
    static async Task Main(string[] args)
    {
        await Task.Delay(3000);

        var sender1 = SenderClient1("john");

        var sender2 = SenderClient1("papa");

        var sender3 = SenderClient1("marry");

        await Task.WhenAll(sender1, sender2, sender3);

        Console.ReadKey();
    }

    static async Task SenderClient1(string nomeUsuario)
    {
        var connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:5005/sockethub", options =>
            {
                options.Headers["Application"] = "API Sender";
            })
            .WithAutomaticReconnect()
            .Build();

        await connection.StartAsync();
        await connection.SendAsync("UpdateClient", nomeUsuario);

        Console.WriteLine("Connection started.");

        connection.Closed += async (error) =>
        {
            await Task.Delay(new Random().Next(0, 5) * 1000);
            await connection.StartAsync();
        };

        while (true)
        {
            Thread.Sleep(400);

            await connection.SendAsync($"SendNotification", $"{nomeUsuario} - {DateTime.Now:G}");

            Console.WriteLine($"Send Message: {nomeUsuario} - {DateTime.Now:G}");
        }
    }
}

More details and an example of the complete solution can be found here: https://github.com/hgmauri/signalr-socket-dotnet5可以在此处找到更多详细信息和完整解决方案的示例: https ://github.com/hgmauri/signalr-socket-dotnet5

You can use the Azure SignalR Service Management SDK to interact with a serverless Azure SignalR Service from a non-Functions app.可以使用Azure SignalR 服务管理 SDK从非函数应用与无服务器 Azure SignalR 服务进行交互。 Their samples have some examples of how to create a negotiate service and how to send messages.他们的示例有一些关于如何创建协商服务以及如何发送消息的示例。 They use an IHostedService to manage the hub contexts, but I bet that could be simplified.他们使用 IHostedService 来管理中心上下文,但我敢打赌这可以简化。

Here's how one would send some messages to clients:以下是向客户发送一些消息的方式:

var serviceManager = new ServiceManagerBuilder().WithOptions(option =>
{
    option.ConnectionString = _connectionString;
    option.ServiceTransportType = _serviceTransportType;
})
.BuildServiceManager();
    
var hubContext = await serviceManager.CreateHubContextAsync("<Your Hub Name>");
    
hubContext.Clients.All.SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM