将 NodeJS 应用程序连接到 SignalR(使用 .NET Core 3)

Connecting NodeJS app to SignalR (with .NET Core 3)

我有一个使用 .NET Core 3 的服务器 运行 SignalR。该项目是从模板开始的,我遵循了指南 (https://docs.microsoft.com/en-gb/aspnet/core/tutorials/signalr?tabs=visual-studio&view=aspnetcore-3.0)。

我已经创建了项目的克隆,并且可以成功连接到服务器并可以按预期接收消息。这也意味着我添加了 CORS。

我希望能够在 Node JS 环境中使用 SignalR,但连接卡在 "Negotiation" 我创建了一个 b运行d 新文件夹,运行 npm init -ynpm i @microsoft/signalr。 创建了一个名为 main.js 的新 js 文件,如下所示:

const signalR = require("@microsoft/signalr");

let connection = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:44336/chathub")
    .configureLogging(signalR.LogLevel.Trace)
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

在 运行 它与 节点之后 main.js 我在控制台

中收到以下错误
[2019-11-26T14:56:14.933Z] Debug: Starting HubConnection.
[2019-11-26T14:56:14.935Z] Debug: Starting connection with transfer format 'Text'.
[2019-11-26T14:56:14.936Z] Debug: Sending negotiation request: http://localhost:44336/chathub/negotiate.
[2019-11-26T14:58:18.890Z] Warning: Error from HTTP request. Error: read ECONNRESET
[2019-11-26T14:58:18.891Z] Error: Failed to complete negotiation with the server: Error: read ECONNRESET
[2019-11-26T14:58:18.892Z] Error: Failed to start the connection: Error: read ECONNRESET
[2019-11-26T14:58:18.892Z] Debug: HubConnection failed to start successfully because of error 'Error: read ECONNRESET'.

好像超时了。服务器、客户端和 nodejs 应用程序都在本地托管。 我确保检查 npm i 安装的信号器版本是否与服务器版本 (3.0.1) 匹配。我什至在 node_modules 中提取了 js 文件并将它们用于另一个客户端(使用 VS 模板制作)并且它可以正常连接。

我不知道如何进一步调试。我尝试使用 VS 连接到服务器,但无法获得任何信息。服务器使用 IIS Express 托管(通过 Visual Studio 启动)。 关于如何进一步调试的任何提示?否则我可能会降级到以前的 .NET Core 版本和另一个信号器版本

我在 VS 中的 startup.cs 代码

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddControllersWithViews();

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                    builder =>
                    {
                        builder
                            .WithOrigins("http://localhost:44399", "http://localhost:44336", "https://localhost:44399", "https://localhost:44336")
                            .AllowCredentials()
                            .AllowAnyMethod()
                            .AllowAnyHeader();
                    });
            });

            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }            

            app.UseRouting();
            app.UseCors("AllowAll");
            app.UseEndpoints(endpoints =>
                {
                    endpoints.MapHub<ChatHub>("/chathub");
                });
        }
    }

您应该考虑您的中间件订单

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowAll",
            builder =>
            {
                builder
                    .WithOrigins("http://localhost:44399",
                        "http://localhost:44336",
                        "https://localhost:44399",
                        "https://localhost:44336")
                    .AllowCredentials()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
            });
    });

    services.AddSignalR();
    services.AddControllersWithViews();
}

试试这个,看看它是否适合你。可以参考我的另一个回答

不知道这是否是根本原因,但我在设置中偶然发现了这一点。

Visual Studio 中 IISExpress 的默认设置不会侦听 httphttps 的同一端口。我在 node.js 文件中使用了 SSL 端口,但使用的是 http 协议。我怀疑你的问题可能是一样的,因为 VS 通常默认为 SSL 端口的 44000 范围。

让我感到困惑的是,我的浏览器在调试期间会在 SSL 端口上弹出。

就我而言,我检查了 ./Properties/launchSettings.json 以获取正在使用的端口:

  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63591",
      "sslPort": 44357
    }
  },

然后相应地更新了我的js文件:

const signalR = require("@microsoft/signalr");

var hubConnection = new signalR.HubConnectionBuilder()
    .configureLogging(signalR.LogLevel.Trace)
    .withUrl("http://localhost:63591/chatHub")
    .build();

瞧。 运行 Visual Studio 2019 年的应用程序,然后在命令行上:

davek-win64% node app.js
[2020-08-05T21:20:15.483Z] Debug: Starting HubConnection.
[2020-08-05T21:20:15.490Z] Debug: Starting connection with transfer format 'Text'.
[2020-08-05T21:20:15.491Z] Debug: Sending negotiation request: http://localhost:63591/chatHub/negotiat
e.
[2020-08-05T21:20:15.591Z] Debug: Selecting transport 'WebSockets'.
[2020-08-05T21:20:15.592Z] Trace: (WebSockets transport) Connecting.
[2020-08-05T21:20:15.615Z] Information: WebSocket connected to ws://localhost:63591/chatHub?id=sYmFd19
_rNCR7q3mddpJBA.
[2020-08-05T21:20:15.616Z] Debug: The HttpConnection connected successfully.
[2020-08-05T21:20:15.616Z] Debug: Sending handshake request.
[2020-08-05T21:20:15.619Z] Trace: (WebSockets transport) sending data. String data of length 32.
[2020-08-05T21:20:15.621Z] Information: Using HubProtocol 'json'.
[2020-08-05T21:20:15.633Z] Trace: (WebSockets transport) data received. String data of length 3.
[2020-08-05T21:20:15.634Z] Debug: Server handshake complete.
[2020-08-05T21:20:15.635Z] Debug: HubConnection connected successfully.
Connected!
[2020-08-05T21:20:28.547Z] Trace: (WebSockets transport) data received. String data of length 74.
Whosebug test
[2020-08-05T21:20:30.637Z] Trace: (WebSockets transport) sending data. String data of length 11.
[2020-08-05T21:20:31.197Z] Trace: (WebSockets transport) data received. String data of length 11.