我有.NET Core 2.2 +带有SignalR的ASP.NET Core 2.2。在我的开发环境中,我具有带有IIS Express 10的Win7。
不幸的是,SignalR没有使用WebSockets,不幸的是,我没有找到强制它使用的方法。
在服务器端,SignalR的配置如下:
app.UseSignalR(
routeBuilder => routeBuilder
.MapHub<ClientsHub>(
"/hubs/clients",
options => options.Transports = HttpTransportType.WebSockets)
);
在客户端配置是:
this._connection = new signalR.HubConnectionBuilder()
.withUrl(this.api.getHubUrl(url), {
transport: HttpTransportType.WebSockets
})
.build();
来自SignalR客户端的协商请求结果如下:
{"connectionId":"1SyteS9TsDE5Q8LBRb2-VA","availableTransports":[]}
结果,SignalR的客户端将此消息写入控制台:
Error: Failed to start the connection: Error: Unable to initialize any of the available transports.
这显然意味着未使用websocket,SignalR无法初始化websocket。
我发现使用Google时,IIS Express默认禁用了websocket,我必须先启用它们。我在文件applicationhost.config的IISExpress / config文件夹中找到了一些设置,并将其设置为“允许”(默认情况下为“拒绝”):
<section name="webSocket" overrideModeDefault="Allow" />
但没有任何改变。
如果要禁用协商,SignalR客户端将尝试通过url直接使用WebSockets,如下所示:
wss://localhost:44360/hubs/clients
但是此请求的结果是错误代码400:
Error during WebSocket handshake: Unexpected response code: 400
是否有可能通过IIS 10强制SignalR使用WebSockets? 还是强制IIS 10允许SignalR使用WebSockets?