我在ASP上的SignalR WebSocket有问题。NET核心API,托管在红树上。
控制台(Chrome浏览器)的完整日志
〔2023-01-11T09:05:21.250Z〕调试:启动HubConnection。signaler.js:457〔2023-01-11T09:05:21.251Z〕调试:启动传输格式为"文本"的连接。signaler.js:457〔2023-01-11T09:05:21.252Z〕调试:发送协商请求:https://localhost:60001/PushMessagesHub/negotiate?negotiateVersion=1.signaler.js:457[2023-01-11T09:05:21.624Z]调试:选择传输"WebSockets"。signaler.js:457[2023-01-11T09:05:21.624Z]跟踪:(WebSockets传输)正在连接。signaler.js:2287到"的WebSocket连接wss://localhost:60001/PushMessagesHub?id=srqV9jQe1zpE5cuTdzzcdg&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJGQ01fVG9rZW4iJGQ01f VE9LRU5fRk9SX0NMSUVOVF9BUFBfUkVHSVNUUkFUSU9OIiwiQXBwbGljYXRpb25JZCI6ImZkMmIwNmE5LTA4YmItNDRkMy04MDQxLWUxYTMyOGVkZTk5ZCIsImV4cCI6MTY3Ng4MzkyMCwiaXNzIjoiaHR0cDovL2xvY2FsaG9 zdC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0LyJ9.ojHw8RS_ZoB0S8w8nlqnfY4uIsit1yrj8iCBURvf2Hw'失败:(匿名)@signar.js:2287signaler.js:453[2023-01-11T09:05:22.027Z]信息:(WebSockets传输)传输出错。signaler.js:447[2023-01-11T09:05:22.027Z]错误:无法启动传输"WebSockets":错误:WebSocket无法连接。在服务器上找不到连接,或者端点可能不是SignalR端点,或者服务器上不存在连接ID,或者存在阻止WebSockets的代理。如果您有多个服务器,请检查是否已启用粘性会话。log@signar.js:447signaler.js:457[2023-01-11T09:05:22.027Z]调试:选择传输"ServerSentEvents"。signaler.js:457〔2023-01-11T09:05:22.027Z〕调试:发送协商请求:https://localhost:60001/PushMessagesHub/negotiate?negotiateVersion=1.signaler.js:457[2023-01-11T09:05:22.040Z]跟踪:(SSE传输)正在连接。:60001/PushMessagesHub?id=2orOj_0FgQIqosu2phE-ow&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJGQ01fVG9rZW4iJGQ01f VE9LRU5fRk9SX0NMSUVOVF9BUFBfUkVHSVNUUkFUSU9OIiwiQXBwbGljYXRpb25JZCI6ImZkMmIwNmE5LTA4YmItNDRkMy04MDQxLWUxYTMyOGVkZTk5ZCIsImV4cCI6MTY3Ng4MzkyMCwiaXNzIjoiaHR0cDovL2xvY2FsaG9 zdC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0LyJ9.ojHw8RS_ZoB0S8w8nlqnfY4uIsit1yrj8iCBURvf2Hw:1加载资源失败:服务器响应为401的状态()signaler.js:447[2023-01-11T09:05:22.045Z]错误:无法启动传输"ServerSentEvents":错误:EventSource无法连接。在服务器上找不到连接,或者服务器上不存在连接ID,或者代理正在拒绝/缓冲连接。如果您有多个服务器,请检查是否已启用粘性会话。log@signar.js:447signaler.js:457[2023-01-11T09:05:22.045Z]调试:选择传输"LongPolling"。signaler.js:457〔2023-01-11T09:05:22.046Z〕调试:发送协商请求:https://localhost:60001/PushMessagesHub/negotiate?negotiateVersion=1.signaler.js:457[2023-01-11T09:05:22.055Z]跟踪:(LongPolling传输)正在连接。signaler.js:457[2023-01-11T09:05:22.055Z]跟踪:(LongPolling传输)轮询:https://localhost:60001/PushMessagesHub?id=k2D5mBMDsZtgjLEbRY2MRg&=1673427922055。signaler.js:457[2023-01-11T09:05:22.077Z]跟踪:(LongPolling传输)轮询:https://localhost:60001/PushMessagesHub?id=k2D5mBMDsZtgjLEbRY2MRg&=1673427922077。signaler.js:457〔2023-01-11T09:05:22.077Z〕调试:HttpConnection连接成功。signaler.js:457〔2023-01-11T09:05:22.077Z〕调试:发送握手请求。signaler.js:457[2023-01-11T09:05:22.078Z]跟踪:(LongPolling传输)发送数据。长度为32的字符串数据。signaler.js:457[2023-01-11T09:05:22.095Z]跟踪:(LongPolling传输)请求完成。响应状态:200。signaler.js:453[2023-01-11T09:05:22.095Z]信息:使用HubProtocol"json"。signaler.js:457[2023-01-11T09:05:22.101Z]跟踪:接收到(LongPolling传输)数据。长度为3的字符串数据。signaler.js:457[2023-01-11T09:05:22.102Z]调试:服务器握手完成。signaler.js:457[2023-01-11T09:05:22.102Z]跟踪:(LongPolling传输)轮询:https://localhost:60001/PushMessagesHub?id=k2D5mBMDsZtgjLEbRY2MRg&_=1673427922102signaler.js:457【2023-01-11T09:05:22.102Z】调试:HubConnection连接成功。site.js?v=B8WTYmxlo2Bbl0nfTLXLzSFGgNFHTS36I85-T8V3fOA:54信号R已连接。
启动.cs(API)
public void ConfigureServices(IServiceCollection services)
{
DependencyInjection.ConfigureDIServices(services, Configuration);
AuthConfig.ConfigureAuth(services, Configuration);
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});
SwaggerConfig.ConfigureSwagger(services);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddControllers();
services.AddControllersWithViews();
services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
}));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLifetime, ILogger<Startup> logger)
{
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCors("CorsPolicy");
app.UseRouting();
app.UseWebSockets();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<PushMessagesHub>("/PushMessagesHub");
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Swagger_Custom_index.html");
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
});
}
signal_client.js(MVC应用程序)
const connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:60001/PushMessagesHub", {
//skipNegotiation: false,
//transport: signalR.HttpTransportType.WebSockets,
//withCredentials: false,
accessTokenFactory: () => {
var bearerToken = $('#bearer_span').text();
return bearerToken;
}
})
.build();
connection.onclose(async () => {
await start();
});
connection.on("PreviewImagesStreamStartedNotification", (deviceId) => {
//StartDownloadingPreviewImagesStream(deviceId);
});
connection.on("PreviewImagesStreamStoppedNotification", (deviceId) => {
//_PreviewStreamCancellationTokenSource.Cancel();
});
start();
async function start() {
try {
await connection.start();
console.log("SignalR Connected.");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
更新11.01.2023.
从控制台(浏览器)添加了完整日志。
WebSocket在从ASP.NET Core MVC应用程序的后端代码连接到SignalR Hub时正常工作,但在从浏览器(JS/jQuery)连接时不工作。在完整控制台日志的末尾,您可以看到SignalR连接到集线器,但协议属于LoongPooling。
问题是由于浏览器限制将JWT与WebSocket/ServerSentEvents一起使用而引起的。
这个问题的解决方案在Microsoft SignalR文档页面上:
问题解决方案