Asp.net-Core

調試時 ASP.NET CORE 2.1 伺服器超時

  • January 25, 2019

‘錯誤:伺服器超時未收到來自伺服器的消息。’。

我正在嘗試調試一些伺服器端程式碼,而當我這樣做時,客戶端會在不到一分鐘的時間內斷開連接。

我只使用 SignalR 與客戶端通信,還沒有控制器。

是否有任何設置可以禁用超時或至少使其比現在更長?

我的launchSettings.json:

{
 "iisSettings": {
   "windowsAuthentication": false,
   "anonymousAuthentication": true,
   "iisExpress": {
     "applicationUrl": "http://localhost:26793",
     "sslPort": 44386
   }
 },
 "profiles": {
   "IIS Express": {
     "commandName": "IISExpress",
     "launchBrowser": true,
     "environmentVariables": {
       "ASPNETCORE_ENVIRONMENT": "Development",
       "ASPNETCORE_HTTPS_PORT": "44386"
     }
   },
   "Api": {
     "commandName": "Project",
     "launchBrowser": true,
     "environmentVariables": {
       "ASPNETCORE_ENVIRONMENT": "Development",
       "ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000"
     }
   }
 }
}

感謝@Arhire Ionut

這是增加Javascript客戶端超時的方法

hubConnection.serverTimeoutInMilliseconds = 100000; // 100 second

更多細節在這裡=> https://github.com/aspnet/Docs/issues/6885

@MahmoudFarhat 在另一個答案中提到的是正確的。但也請查看此連結,然後閱讀下面的評論。

如果 signalR 斷開連接,您應該再次嘗試重新建立連接。連接可能由於其他幾個原因而斷開,包括使用者切換網路。例如,如果使用者正在使用手機並連接到家庭/辦公室 Wifi,但退出後連接到蜂窩數據連接。

要重新連接,您可以使用以下內容(對我來說就像一個魅力):

// re-establish the connection if connection dropped
connection.onclose(() => setTimeout(startSignalRConnection(connection), 5000));

哪裡startSignalRConnection是:

const startSignalRConnection = connection => connection.start()
 .then(() => console.info('Websocket Connection Established'))
 .catch(err => console.error('SignalR Connection Error: ', err));

和連接是

const connection = new HubConnectionBuilder()
 .withUrl(connectionHub, options)
 .withHubProtocol(protocol)
 .build();

引用自:https://stackoverflow.com/questions/49816245