# Microsoft.AspNetCore.SignalR.HubException: 与服务器协商失败。协商请求在 30 秒后超时。

- **ID:** `dotnet/signalr-negotiate-timeout`
- **领域:** dotnet
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

SignalR 客户端未能在默认超时时间内完成协商握手，通常是由于网络延迟、服务器过载或防火墙阻止 WebSocket 升级。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 6.0 | active | — | — |
| 7.0 | active | — | — |
| 8.0 | active | — | — |

## 解决方案

1. ```
   Increase the negotiation timeout on the client: in JavaScript, use 'const connection = new signalR.HubConnectionBuilder().withUrl('/hub').configureLogging(signalR.LogLevel.Information).build();' and set 'connection.serverTimeoutInMilliseconds = 60000;' before starting.
   ```
2. ```
   Optimize server-side negotiation by reducing the number of transports offered: in Program.cs, use 'app.MapHub<MyHub>("/hub", options => { options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents; });' to skip slow transport negotiation.
   ```

## 无效尝试

- **** — Increasing the client timeout alone may mask underlying network issues; the server must also handle long-running negotiations. (60% 失败率)
- **** — Disabling WebSocket transport forces fallback to long polling, which can degrade performance and still timeout if the server is overloaded. (50% 失败率)
