Jea*_*ker 3 c# signalr reactjs asp.net-core .net-6.0
我想将 .NET Core 5 React 项目迁移到 .NET 6,但我遇到了 SignalR 的一些问题。
我已按照此Microsoft 文档中的文章一步步使用 .NET 6 实现 SignalR。但我仍然遇到与此处所示相同的错误:SignalR 错误日志
任何帮助将不胜感激。我的代码片段:
程序.cs
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();
Run Code Online (Sandbox Code Playgroud)
聊天中心.cs
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
Run Code Online (Sandbox Code Playgroud)
布局.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();
Run Code Online (Sandbox Code Playgroud)
安装代理.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);
};
Run Code Online (Sandbox Code Playgroud)
Jea*_*ker 11
对于任何未来为此苦苦挣扎的人。一旦我从代理中间件中删除了标头并添加了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);
};
Run Code Online (Sandbox Code Playgroud)
到
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);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2741 次 |
| 最近记录: |