Detect 'server offline' with SignalR

梦想的初衷 提交于 2019-12-23 11:40:47

问题


We are attempting to use SignalR in an low-bandwidth environment where the connection to the backend server can come and go randomly, and we want our web application to respond appropriately.

It looks like this connection API has been in flux over the past year, but in accordance with the latest documentation, I have tried hooking up to $.connection.hub.stateChanged to detect changes in the connection status, and I get a couple hits on startup when the client goes from disconnected -> connecting -> connected, but when I stop the server, the event handler does not fire, and when I start the server again, the real-time messaging is no longer working.

To test this scenario, I am on Windows 7 running the ASP.NET web server in IIS and the web client is running in Google Chrome. Once the site is running, and messages are being exchanged, I kill the website in IIS, and the messages stop but the client does not receive any notification.

Here's a quick snippet (using TypeScript):

$.connection.hub.stateChanged((change) => {
   console.log("state changed...");
   console.log(change);

   if (change.newState === $.signalR.connectionState.reconnecting) {
      this.raise('action:disconnected');
   }
   else if (change.newState === $.signalR.connectionState.connected) {
      this.raise('action:connected');
   }
});

Any guidance would be much appreciated, thanks!

-Jeremy


回答1:


What version of SignalR are you using? In the latest (and ones before it) the server going offline is immediately detected and the state change is raised with the reconnecting flag.

To kill the server, I assume you mean shut down IIS .e.g iisreset. Stopping the website in inetmgr does NOT shut the site down, it merely removes the Http.sys binding for that port. See https://github.com/SignalR/SignalR/issues/1148 for more info.

If you want to simulate the server going down kill w3wp.



来源:https://stackoverflow.com/questions/14531450/detect-server-offline-with-signalr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!