Reputation: 271
Working with a WebSocket server using Microsoft.Web.WebSockets.WebSocketHandler where I'm keeping the connected clients in a WebSocketCollection. When the client loses its network connection, at the server the OnClose and OnError methods are not called. Additionally, looking at the specific client in the collection shows that its WebSocket.State is still described as connected. This is on IIS8.
Short of manually sending the client a message and looking for a response, is there another way to determine that the client has dropped of the network? Is there some IIS configuration I'm missing? Thanks.
Upvotes: 10
Views: 11848
Reputation: 271
I was able to solve my problem with two steps. First, I ran the following commands at the prompt to edit the IIS config.
cd \Windows\System32\inetsrv
appcmd unlock config /section:system.webServer/websocket
Then I updated my Web.config with the following.
<system.webServer>
<webSocket enabled="true" pingInterval="00:00:05" />
<system.webServer />
For IIS Express and VS projects, you have to edit "applicationhost.config" file and change overrideModeDefault="Allow"
<configSections>
<sectionGroup name="system.webServer">
...
<section name="webSocket" overrideModeDefault="Allow" />
</sectionGroup>
<configSections>
Upvotes: 17
Reputation: 22011
This is TCP: when you don't send or receive, how can you know if the peer is still there or gone?
If you kill the peer process or reboot it's machine, that peer's OS networking stack will usually actively close the TCP, so you immediately detect the lost WebSocket detection also. But if you just pull the plug on the peer (power or network) or something else severe happens, the TCP isn't actively closed.
In the context of WebSocket, you can do WebSocket-level pings/pongs (don't know if IIS supports that .. WS ping/pong heartbeating) or you can do app-level heartbeats by sending small WS messages.
Upvotes: 0