Mat*_*nta 2 c# websocket azure-web-sites asp.net-core asp.net-core-signalr
我正在研究使用以下堆栈的SignalR Core Web应用程序:
客户端使用SignalR NPM包(@ aspnet/signalr).
该应用程序配置如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(
"CorsPolicy",
builder => builder
.AllowCredentials()
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddSignalR();
}
public void Configure(IApplicationBuilder app)
{
app.UseCors("CorsPolicy");
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseWebSockets();
app.UseSignalR(routes =>
{
routes.MapHub<ClientHub>("/hubs/notifications");
});
}
Run Code Online (Sandbox Code Playgroud)
在本地运行时http://localhost,客户端使用WS协议进行连接.但是,当部署在Azure App Service中时,它会回退到SSE.
浏览器日志显示:
WebSocket connection to 'wss://xxxxxx.azurewebsites.net/hubs/notifications?id=ZRniWKpMLMPIyLhS5RSyAg' failed: Error during WebSocket handshake: Unexpected response code: 503
Information: SSE connected to https://xxxxxx.azurewebsites.net/hubs/notifications?id=ig47oOdQzbasdgrlr0cHaw
Run Code Online (Sandbox Code Playgroud)
该negotiate方法似乎返回对Websockets的支持:
{"connectionId":"ZRniWKpMLMPIyLhS5RSyAg",
"availableTransports":[
{"transport":"WebSockets","transferFormats":["Text","Binary"]},
{"transport":"ServerSentEvents","transferFormats":["Text"]},
{"transport":"LongPolling","transferFormats":["Text","Binary"]}]
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?或者WSS尚未获得支持?
要回答我自己的问题,Websocket连接中的失败不是ASP.NET Core或堆栈的问题,而是由于Azure App Service需要在应用程序设置中启用Websockets :
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |