gpt4 book ai didi

SignalR SSE 握手失败

转载 作者:行者123 更新时间:2023-12-03 14:56:47 28 4
gpt4 key购买 nike

在“Windows Server 2008 R2”上托管一个 ASP.Net Core 2.1 webapi 和一个 Angular 6 webapp,使用相同的域名但不同的端口(分别为:81 和 :80);不包含在同一个文件夹中。

根据this article , SSE 是 Websocket 不可用时的(标准)回退,这是预期的,因为服务器不支持 Websocket,因为它们是在 IIS 8.0 (server is IIS 7.5) 中引入的

在开发中,一切都完美无缺。但是,发布后,浏览器控制台会在 15 秒后报告以下内容(可能是默认的“HandshakeTimeout”时间)。

> Information: SSE connected to http://my.website.com:81/notify?id=kaSkcGUjcZD4ylJAC7B70A
> Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
> Error: Not Found

Startup.cs 当前设置如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors(o => {
o.AddPolicy("Limited", builder => builder
.WithOrigins("http://my.website.com") // .AllowAnyOrigin() also tested and worked
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
});
services.AddDbContext<DatabaseContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("Database"));
});
}

public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseCors("Limited");
app.UseHttpsRedirection();
app.UseMvc();
app.UseSignalR(routes => routes.MapHub<NotifyHub>("/notify"));
}

SignalR Hub 几乎是空的(事件推送在其他地方处理):
public class NotifyHub : Hub
{
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
}

public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
}
}

Angular 服务设置如下:
import { Injectable } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';
import * as signalR from '@aspnet/signalr';
import { Subject } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class RefreshService {
private connection: HubConnection;
public timestamp = new Subject<string>();

constructor(private zone: NgZone) {
this.connection = new signalR.HubConnectionBuilder()
.withUrl(http://my.website.com:81/notify)
.build();

this.connection
.start()
.then(() => {
this.connection.on('Message', data => {
// Do Work
});
})
.catch(err => console.error(err.toString()));
}
}

所有 NuGet 包都是最新的(v2.1.1 适用于除 Microsoft.NETCore.App 之外的所有内容,即 v2.1.0)并使用 v1.0.2(最新) ASP.NET SignalR NPM package -- 所以服务器端和客户端 SignalR 是相同的。

使用静态 IP 地址,我能够让应用程序连接到本地发布的 API(但运行 IIS 10.0)。

有什么我想念的吗?很多技术都是相当新的,但我还不想假设它们有什么问题。

最佳答案

如果有人仍然面临这个问题,我通过在 IIS 上配置 IIS 8.0 WebSocket 协议(protocol)支持来解决它。
Microsoft documentation

关于SignalR SSE 握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820679/

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