【发布时间】:2020-05-14 03:27:25
【问题描述】:
在下面不断收到此错误:
localhost:3000/negotiate?negotiateVersion=1 404(未找到) 错误:无法完成与服务器的协商:错误:未找到 错误:无法启动连接:错误:未找到
这是软件版本
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="5.0.0-``
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
</ItemGroup>
---------客户--------
"dependencies": {
"@microsoft/signalr": "^3.1.4",
这是我的 Cors.cs。我试过不包括 .WithOrigins 或更改端口,但它没有帮助。
public static void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAllCors", builder =>
{
builder
.WithOrigins("https://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.SetIsOriginAllowed(delegate (string requestingOrigin)
{
return true;
}).Build();
});
});
public static void Configure(IApplicationBuilder app)
{
app.UseCors("AllowAllCors");
app.UseSignalR(routes =>
{
routes.MapHub<ChatHub>("/client/messages");
});
}
--
import React, { Component } from 'react';
import * as signalR from '@microsoft/signalr';
class EmailEditor extends Component {
constructor(props) {
super(props);
this.state = {
nick: '',
message: '',
messages: [],
hubConnection: null,
};
}
componentDidMount = () => {
const nick = window.prompt('Your name:', 'John');
const hubConnection = new signalR.HubConnectionBuilder()
.withUrl("/client/messages")
.configureLogging(signalR.LogLevel.Information)
.build();
this.setState({ hubConnection, nick }, () => {
this.state.hubConnection.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('+ err));
this.state.hubConnection.on('ReceiveMessage', (nick, receivedMessage) => {
const text = `${nick}: ${receivedMessage}`;
const messages = this.state.messages.concat([text]);
this.setState({ messages });
});
});
};
【问题讨论】:
-
在 .NET Core 上,您不需要 SignalR 包,因为 SignalR 在 .NET Core 3.1 上是本机的。
标签: reactjs asp.net-core signalr signalr.client asp.net-core-signalr