Javascript 将NodeJS应用程序连接到信号器(使用.NET Core 3)
我有一个使用.NETCore3运行Signaler的服务器。项目是从模板开始的,我遵循了一个指南() 我已经创建了项目的克隆,可以成功连接到服务器,并可以按预期接收消息。这也意味着我添加了CORS 我希望能够在Node JS环境中使用SignalR,但连接在“协商”时受阻 我已经创建了一个全新的文件夹,运行了Javascript 将NodeJS应用程序连接到信号器(使用.NET Core 3),javascript,c#,node.js,.net-core,signalr,Javascript,C#,Node.js,.net Core,Signalr,我有一个使用.NETCore3运行Signaler的服务器。项目是从模板开始的,我遵循了一个指南() 我已经创建了项目的克隆,可以成功连接到服务器,并可以按预期接收消息。这也意味着我添加了CORS 我希望能够在Node JS环境中使用SignalR,但连接在“协商”时受阻 我已经创建了一个全新的文件夹,运行了npminit-y和npmi@microsoft/signal。 创建了一个名为main.js的新js文件,如下所示: const signalR = require("@microsoft
npminit-y
和npmi@microsoft/signal
。
创建了一个名为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应用程序都在本地托管。
我确保检查安装了npmi
的signar版本是否与服务器版本(3.0.1)匹配。我甚至提取了node_模块中的js文件,并将它们用于另一个客户端(使用VS模板制作),它可以很好地连接
我不知道如何进一步调试。我尝试使用VS连接到服务器,但无法获取任何信息。服务器使用IIS Express托管(通过Visual Studio启动)。
关于如何进一步调试的任何提示?否则,我可能会使用另一个signer版本降级到以前的.NET核心版本
VS中的My 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");
});
}
}
公共类启动
{
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
//services.AddControllersWithViews();
services.AddCors(选项=>
{
options.AddPolicy(“AllowAll”,
生成器=>
{
建设者
.来源(“http://localhost:44399", "http://localhost:44336", "https://localhost:44399", "https://localhost:44336")
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.AddSignalR();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
附录UseCors(“AllowAll”);
app.UseEndpoints(端点=>
{
endpoints.MapHub(“/chathub”);
});
}
}
> p>你应该考虑你的中间件订单< /p>
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的默认设置不在同一端口上侦听http
和https
。我在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.
stackoverflow 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.
如何配置startup.cs添加了startup.cs类!我有一个与OP相同的问题,不幸的是,重新排序这个生成器似乎没有做任何事情。我得到错误:无法完成与服务器的协商:错误:每次都读取ECONNRESET
。我想我已经解决了。尝试使用非安全协议访问SSL端口不是错误:)