gpt4 book ai didi

c# - websocket 握手期间 asp.net core signalr 错误

转载 作者:行者123 更新时间:2023-12-02 16:30:12 32 4
gpt4 key购买 nike

抱歉我的英语不好。我正在尝试从 SPA 应用程序 (Vue.JS) 连接到 SignalR Hub。

客户端代码:

this.hubConnection = new HubConnectionBuilder()
.withUrl('http://localhost:*****/testhub')
.configureLogging(LogLevel.Information)
.build()

我收到错误:错误:无法完成与服务器的协商:语法错误:位置 0 处的 JSON 中出现意外标记

然后我尝试了:

this.hubConnection = new HubConnectionBuilder()
.withUrl('http://localhost:*****/testhub', {
skipNegotiation: true,
transport: HttpTransportType.WebSockets
})
.configureLogging(LogLevel.Information)
.build()

没有任何改变(但我收到另一个错误):

与“ws://localhost:50523/testhub”的 WebSocket 连接失败:WebSocket 握手期间出错:意外响应代码:200

我在 Startup.cs 中检查了 UseWebsockets。我也尝试在客户端更改SignalR版本,但无法解决问题。当我尝试在多页应用程序中连接 SignalR 时,我看不到它。我第一次就拥有了。

我做错了什么?

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using Vue2Spa.Models;
using Vue2Spa.Controllers.App;
using Vue2Spa.Hubs;

namespace Vue2Spa
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = @"Server=.\SQLEXPRESS;Database=****;Trusted_Connection=True;";
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connection));

services.AddIdentity<Account, IdentityRole>(options =>
options.SignIn.RequireConfirmedEmail = true)
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();

//JWT
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = AuthOptions.ISSUER,
ValidateAudience = true,
ValidAudience = AuthOptions.AUDIENCE,
ValidateLifetime = true,
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
ValidateIssuerSigningKey = true,
};
})


services.AddAuthorization(options =>
{

});
/*services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build();
});*/

// Simple example with dependency injection for a data provider.
services.AddSingleton<Providers.IWeatherProvider, Providers.WeatherProviderFake>();

//File's parameters
services.Configure<FormOptions>(o => {
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = int.MaxValue;
o.MemoryBufferThreshold = int.MaxValue;
});

services.AddSignalR();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

// Webpack initialization with hot-reload.
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true,
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");

routes.MapRoute(
name: "default1",
template: "{controller=Admin}/{action=Index}/{id?}");

routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });

routes.MapSpaFallbackRoute(
name: "spa-fallback1",
defaults: new { controller = "Admin", action = "Index" });
});
app.UseAuthentication();
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Xss-Protection", "1");
await next();
}).UseSignalR(route =>
{
route.MapHub<ChatHub>("/chat");
});
}
}
}

最佳答案

问题已经解决了。我将 UseSignalR 移至 UseMvc 之上。

关于c# - websocket 握手期间 asp.net core signalr 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965762/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com