我想迁移我的电脑.NET核心5对项目做出react .NET6,但我在信号机方面遇到了一些问题.

从这Microsoft Docs篇文章开始,我循序渐进地介绍了如何实现SignalR.净6.但我还是得到了和这里一样的错误:SignalR Error Log

任何帮助都将不胜感激.我的代码片段:

程序反恐精英

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();

builder.Services.AddSignalR();

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        builder =>
        {
            builder.WithOrigins("https://localhost:44413/")
                .AllowAnyHeader()
                .WithMethods("GET", "POST")
                .AllowCredentials();
        });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.UseCors();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html");
app.MapHub<ChatHub>("/chatHub");

app.Run();

聊天中心.反恐精英

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

布局js

const connection = new SignalR.HubConnectionBuilder()
  .withUrl("/chathub")
  .configureLogging(SignalR.LogLevel.Information)
  .build();

async function start() {
  try {
    console.log("SignalR Connecting....");
    await connection.start();
    console.log("SignalR Connected.");
  } catch (err) {
    console.log(err);
    setTimeout(start, 5000);
  }
}

connection.onclose(async () => {
  await start();
});

// Start the connection.
start();

设置代理.js

const createProxyMiddleware = require("http-proxy-middleware");
const { env } = require("process");

const target = env.ASPNETCORE_HTTPS_PORT
  ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}`
  : env.ASPNETCORE_URLS
  ? env.ASPNETCORE_URLS.split(";")[0]
  : "http://localhost:9738";

const context = ["/weatherforecast", "/chathub"];

module.exports = function (app) {
  const appProxy = createProxyMiddleware(context, {
    target: target,
    secure: false,
    headers: {
      Connection: "Keep-Alive",
    },
  });

  app.use(appProxy);
};

推荐答案

对于任何在future 挣扎的人.有一次,我从代理中间件中删除了头并添加了ws: true个(启用websockets).一切如期开始.

从…起

const createProxyMiddleware = require("http-proxy-middleware");
const { env } = require("process");

const target = env.ASPNETCORE_HTTPS_PORT
  ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}`
  : env.ASPNETCORE_URLS
  ? env.ASPNETCORE_URLS.split(";")[0]
  : "http://localhost:9738";

const context = ["/weatherforecast", "/chathub"];

module.exports = function (app) {
  const appProxy = createProxyMiddleware(context, {
    target: target,
    secure: false,
    headers: {
      Connection: "Keep-Alive",
    },
  });

  app.use(appProxy);
};

const createProxyMiddleware = require("http-proxy-middleware");
const { env } = require("process");

const target = env.ASPNETCORE_HTTPS_PORT
  ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}`
  : env.ASPNETCORE_URLS
  ? env.ASPNETCORE_URLS.split(";")[0]
  : "http://localhost:9738";

const context = ["/weatherforecast", "/chathub"];

module.exports = function (app) {
  const appProxy = createProxyMiddleware(context, {
    target: target,
    secure: false,
    ws: true, // <-- Add this
    headers: { // <-- Remove this
      Connection: "Keep-Alive", // <-- Remove this
    }, // <-- Remove this
  });

  app.use(appProxy);
};

Csharp相关问答推荐

如何使用while循环实现异常处理

在FilePath中搜索一个词,并根据First Match从左到右提取文件路径

此反射有什么问题.是否发送值转换委托?

Nuget包Serilog.Sinks.AwsCloudwatch引发TypeLoadExceptions,因为父类型是密封的

JSON空引用异常仅在调试器中忽略try-Catch块,但在其他上下文中捕获得很好

MigraDoc文档

将字节转换为 struct 并返回

CA1508:';NULL=>;TRUE;始终为';TRUE';.移除或重构条件(S)以避免死代码

如何在我的C#应用程序中设置带有reactjs前端的SignalR服务器?

在DoubleClick上交换DataGridViewImageColumn的图像和工具提示

如何从另一个类的列表中按ID取值

C#LINQ延迟执行和嵌套方法

基于C#方法的EF核心过滤查询(缓冲与流)

为什么我在使用有效令牌的情况下仍未获授权?

根据优先级整理合同列表

无法使用直接URL通过PictureBox.ImageLocation加载图像

Excel将';@';添加到具有范围的公式中

如何获取我在SQL中输入的值

具有双返回类型的多路委托,仅返回最后一个方法';S结果,而不是所有方法';S结果

.NET核心使用Swagger/OpenAPI生成文档,如何用不同的语言生成?