使用 https 从 NodeJS 17.9.1 升级到 18.0.0 后,SignalR 连接失败

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

我有一个简单的 NodeJS 应用程序,如下所示
index.js

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

//Works if I disable TLS validation
//process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; 

let connection = new signalR.HubConnectionBuilder()
      .withUrl("https://myapp.mydomain.com:5000/messageHub",
     //.withUrl("http://myapp.mydomain.com:5002/messageHub", //works if I use http
    {
         withCredentials: false
    })
    .withAutomaticReconnect()
    .configureLogging(signalR.LogLevel.Information)
    .build();
    connection.logging = true;
    async function start() {
    try {
        await connection.start({ withCredentials: false });
        console.log("SignalR Connected.");
    } catch (err) {
        console.log(err);
        setTimeout(start, 5000);
    }
};

connection.onclose(async () => {
    await start();
});

start();

package.json

{
  "name": "Client",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@microsoft/signalr": "8.0.0"
  }
}

  • 它在 Node 17.9.1(或更早版本)上工作正常,在 Node 18.0.0(或更高版本)上失败
  • 如果我使用http而不是https,它就可以工作
  • 如果我禁用 TLS 验证,它会起作用,但我当然不想这样做
    process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; 

我的服务器在 .NET 8 上使用 SignalR 8。

当我运行应用程序时

node index.js
我明白了

[2024-02-08T11:06:44.246Z] Error: Failed to complete negotiation with the server: TypeError: fetch failed
[2024-02-08T11:06:44.246Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: fetch failed
FailedToNegotiateWithServerError: Failed to complete negotiation with the server: TypeError: fetch failed
    at HttpConnection._getNegotiationResponse (C:\dev\20240227electron\node_modules\@microsoft\signalr\dist\cjs\HttpConnection.js:257:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async HttpConnection._startInternal (C:\dev\20240227electron\node_modules\@microsoft\signalr\dist\cjs\HttpConnection.js:170:41)
    at async HttpConnection.start (C:\dev\20240227electron\node_modules\@microsoft\signalr\dist\cjs\HttpConnection.js:73:9)
    at async HubConnection._startInternal (C:\dev\20240227electron\node_modules\@microsoft\signalr\dist\cjs\HubConnection.js:135:9)
    at async HubConnection._startWithStateTransitions (C:\dev\20240227electron\node_modules\@microsoft\signalr\dist\cjs\HubConnection.js:112:13)
    at async Timeout.start [as _onTimeout] (C:\dev\20240227electron\index.js:18:9) {

有什么想法吗?

node.js signalr
1个回答
0
投票

在版本 18 中,添加了限制

server.requestTimeout
。看来需要把时间设置长一点。

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