SignalR 客户端尝试在服务器端运行 NodeJS

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

我正在尝试在服务器端 NodeJS 上运行 Signalr 客户端。但我收到以下错误。我该如何解决这个问题?
Chat.js 代码

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

const connection = new signalR.HubConnectionBuilder()
  .withUrl("http://127.0.0.1:7265/chathub")
  .configureLogging(signalR.LogLevel.Information)
  .build();

const start = async () => {
  try {
    await connection.start();
    console.log("Connected to signal r hub");
  } catch (error) {
    console.log(error);
  }
};
// starting the app
const startApp = async () => {
  await start(); // connection will stablised
};

startApp();

我收到错误消息

[2023-04-08T07:27:03.490Z] Error: Failed to complete negotiation with the server: FetchError: request to http://127.0.0.1:7265/chathub/negotiate?negotiateVersion=1 failed, reason: connect ECONNREFUSED 127.0.0.1:7265
[2023-04-08T07:27:03.490Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: FetchError: request to http://127.0.0.1:7265/chathub/negotiate?negotiateVersion=1 failed, reason: connect ECONNREFUSED 127.0.0.1:7265
FailedToNegotiateWithServerError: Failed to complete negotiation with the server: FetchError: request to http://127.0.0.1:7265/chathub/negotiate?negotiateVersion=1 failed, reason: connect ECONNREFUSED 127.0.0.1:7265
    at HttpConnection._getNegotiationResponse (/Users/user/Desktop/WebChat-Client-master/node_modules/@microsoft/signalr/dist/cjs/HttpConnection.js:256:35)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async HttpConnection._startInternal (/Users/user/Desktop/WebChat-Client-master/node_modules/@microsoft/signalr/dist/cjs/HttpConnection.js:172:41)
    at async HttpConnection.start (/Users/user/Desktop/WebChat-Client-master/node_modules/@microsoft/signalr/dist/cjs/HttpConnection.js:75:9)
    at async HubConnection._startInternal (/Users/user/Desktop/WebChat-Client-master/node_modules/@microsoft/signalr/dist/cjs/HubConnection.js:132:9)
    at async HubConnection._startWithStateTransitions (/Users/user/Desktop/WebChat-Client-master/node_modules/@microsoft/signalr/dist/cjs/HubConnection.js:109:13)
    at async start (/Users/user/Desktop/WebChat-Client-master/chat.js:10:5)
    at async startApp (/Users/user/Desktop/WebChat-Client-master/chat.js:18:3) {
  errorType: 'FailedToNegotiateWithServerError'
}
javascript node.js signalr
1个回答
0
投票

你告诉它连接到

http://127.0.0.1:7265/chathub
但连接被拒绝了。

任一:

  • 你忘了运行服务器
  • 你把服务器的 URL 弄错了
  • 防火墙正在阻止连接(这对于本地主机 URL 来说不太可能)。

运行服务器或更正 URL。

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