You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js服务端运行SignalR客户端遇连接拒绝错误,求解决方法

Node.js SignalR客户端连接被拒绝(ECONNREFUSED)解决方案

问题描述

尝试在Node.js环境运行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'
}

解决步骤

报错核心是connect ECONNREFUSED 127.0.0.1:7265,说明客户端无法连接到指定地址的SignalR服务端,按以下步骤排查:

  • 确认服务端是否运行
    检查7265端口是否被占用:

    • Mac/Linux执行:lsof -i :7265
    • Windows执行:netstat -ano | findstr :7265
      如果无输出,说明SignalR服务端未启动,先启动服务端再试。
  • 验证服务端Hub路由配置
    手动在浏览器访问http://127.0.0.1:7265/chathub/negotiate,如果返回JSON格式的协商数据,说明路由配置正常;如果404或无法访问,检查服务端的Hub注册代码(比如ASP.NET Core中需确保app.MapHub<ChatHub>("/chathub");已正确添加)。

  • 核对连接地址正确性

    • 确认服务端实际监听的端口和协议(比如服务端用HTTPS的话,客户端地址要改成https://127.0.0.1:7265/chathub)
    • 若服务端和客户端不在同一机器,替换127.0.0.1为服务端的实际IP或域名。
  • 处理跨域问题(若存在跨域场景)
    如果客户端Node.js服务和SignalR服务端不在同一域名/端口,需在服务端配置CORS,允许客户端的请求来源。以ASP.NET Core为例,需添加:

    builder.Services.AddCors(options =>
    {
        options.AddPolicy("AllowNodeClient", policy =>
        {
            policy.WithOrigins("http://你的客户端地址")
                  .AllowAnyHeader()
                  .AllowAnyMethod()
                  .AllowCredentials();
        });
    });
    
    app.UseCors("AllowNodeClient");
    
  • 添加重连逻辑(可选优化)
    为客户端添加断开自动重连逻辑,提升稳定性:

    connection.onclose(async () => {
      console.log("连接断开,5秒后尝试重连...");
      setTimeout(start, 5000);
    });
    

内容的提问来源于stack exchange,提问作者jalil_cse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:15:35