Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在两个ASP.NET Core 2.1 Razor web应用程序中使用SignalR作为中心和客户端_C#_Asp.net Core_Signalr_Asp.net Core Signalr - Fatal编程技术网

C# 在两个ASP.NET Core 2.1 Razor web应用程序中使用SignalR作为中心和客户端

C# 在两个ASP.NET Core 2.1 Razor web应用程序中使用SignalR作为中心和客户端,c#,asp.net-core,signalr,asp.net-core-signalr,C#,Asp.net Core,Signalr,Asp.net Core Signalr,我有2个ASP.NET核心Razor web应用程序。每个应用程序都将使用信号器与web应用程序客户端和移动客户端进行通信。由于我的预期用途,我已将web应用程序设置为集线器和客户端,其中客户端正在使用.NET SignalR客户端。两个web应用中的每一个都具有以下功能: static internal HubConnection Connection; // In the Startup class public Startup(IConfiguration

我有2个ASP.NET核心Razor web应用程序。每个应用程序都将使用信号器与web应用程序客户端和移动客户端进行通信。由于我的预期用途,我已将web应用程序设置为集线器和客户端,其中客户端正在使用.NET SignalR客户端。两个web应用中的每一个都具有以下功能:

        static internal HubConnection Connection; // In the Startup class

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Connection = new HubConnectionBuilder()
                 // Where the URL is for the OTHER web app
                .WithUrl("https://localhost:44386/NotificationHub")
                .Build();
        }
每个项目在Hubs文件夹中还有一个从Hub派生的NotificationHub类

在每个应用程序的Startup ConfigureServices方法中,我将其作为最后一条语句:

services.AddSignalR();
在每个应用程序的启动配置方法中,在调用UseMvc之前,我有以下内容:

app.UseSignalR(routes =>
{
    routes.MapHub<NotificationHub>("/NotificationHub");
});
因此,我不确定最初如何从一个客户端连接到集线器,也不确定是否正确使用了URL。

使用js客户端:

1) 安装信号机npm:
npm安装@aspnet/signer

2) 在所需页面中添加引用

3) 添加连接对象代码

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/NotificationHub")
    .configureLogging(signalR.LogLevel.Information)
    .build();
connection.start().catch(err => console.error(err.toString()));
4) 调用您需要的方法

connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
对于.Net客户端

1) 安装nuget:
安装软件包Microsoft.AspNetCore.signer.Client

(二)

您可以通过单击按钮或发送消息来启动连接

您可以在以下链接中找到更多详细信息:


您想使用.net客户端还是使用js进行连接?为了发布我应该提到的答案,我想在两个appsOK中都使用.NET客户端,但是我应该将调用放在哪里:wait connection.StartAsync()?如果您希望始终保持连接,您可以添加事件处理程序或仅在ctor中。到目前为止,您可以通过I管理使集线器和客户端朝一个方向工作。我还没有在每个web应用程序中测试运行集线器和客户端。一个显而易见的问题出现了-我如何确定StartAsync是否实际连接?很高兴在这里帮助您。。。别忘了标记是否保存了您的一天:)
connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
 HubConnection connection  = new HubConnectionBuilder()
            .WithUrl("https://localhost:44386/NotificationHub")
            .Build();     

 await connection.StartAsync();

 await connection.InvokeAsync("SendMessage", 
                    "user", "message");