SignalRNetCore3.1React.Js ~negotiate?negotiateVersion=1 404 (Not Found) (相信是CORS问题)

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

一直收到下面这个错误。

localhost:3000negotiate?negotiateVersion=1 404 (Not Found)Error: 未能完成与服务器的协商。错误。Not FoundError.Failed to start the connection: 错误:未找到 Failed to start the connection: 错误:未找到 未找到

这是软件版本

  <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 });
      });
    });
  };
reactjs asp.net-core signalr signalr.client asp.net-core-signalr
1个回答
1
投票

服务器的端口在5001上,因此它必须是...。

const hubConnection = new signalR.HubConnectionBuilder()
      .withUrl("https://localhost:5001/client/messages")
      .configureLogging(signalR.LogLevel.Information)
      .build();
© www.soinside.com 2019 - 2024. All rights reserved.