部署到Prod时,WebSockets不能与SignalR Core一起使用 [英] WebSockets don't work with SignalR Core when deployed to Prod

查看:494
本文介绍了部署到Prod时,WebSockets不能与SignalR Core一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IIS 8.5后面运行的ASP.NET Core 2.1

ASP.NET Core 2.1 running behind IIS 8.5

SignalR在连接时似乎没有协商子协议,因此无法完成连接.从客户端发送到服务器的消息永远不会收到.

SignalR doesn't seem to negotiate a Sub-Protocol when connecting and fails to complete the connection. Messages sent from the client to the server are never received.

我无法将站点配置为直接从Kestrel提供服务,因为这是共享服务器. 将传输方式更改为长时间轮询. 我尝试禁用HTTPS/HSTS重定向.

I can't configure the site to serve from Kestrel directly as this is a shared server. Changing the transport to long-polling works. I have tried disabling HTTPS/HSTS redirection.

随附来自客户端和服务器的日志:

Attached are the logs from the client and the server:

客户日志:

Information: Normalizing 'hubs/screen' to 'https://<url>/hubs/screen'.
Utils.ts:173 Information: WebSocket connected to ws://<url>/hubs/screen?id=
...
Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'.<id>

服务器日志:

dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionManager[1]
      New connection <id> created.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionDispatcher[10]
      Sending negotiation response.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionDispatcher[4]
      Establishing new connection.
dbug: Microsoft.AspNetCore.SignalR.HubConnectionHandler[5]
      OnConnectedAsync started.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[1]
      Socket opened using Sub-Protocol: '(null)'.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[14]
      Socket connection closed prematurely.
System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. ---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
   at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at System.Net.WebSockets.WebSocket.ReceiveAsync(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport.StartReceiving(WebSocket socket)
fail: Microsoft.AspNetCore.SignalR.HubConnectionContext[5]
      Failed connection handshake.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[4]
      Waiting for the application to finish sending data.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[2]
      Socket closed.

推荐答案

我发现了问题!

我们有一个自定义IIS模块,该模块将服务器名称+请求计时作为HTTP标头注入,这导致SignalR失败. (该模块可能只是盲目地注入其标头,而不考虑Websockets)

We had a custom IIS module that injected server name + request timing as an HTTP header and this was causing SignalR to fail. (The module was probably just blindly injecting its header, without respect to Websockets)

这篇关于部署到Prod时,WebSockets不能与SignalR Core一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆