In my Windows 8 JS application, I have a web socket object defined like this:
var webSocket = new Windows.Networking.Sockets.MessageWebSocket();
webSocket.control.messageType = Windows.Networking.Sockets.SocketMessageType.utf8;
webSocket.onmessagereceived = that._onMessageReceived;
webSocket.onclosed = that._onClosed;
I connect using webSocket.connectAsync(uri).done(/* ... */)
and that part works fine.
If I stop my web server on the other end, my application doesn't get notified and thinks the connection is still alive. The 'closed' event never fires, and there doesn't seem to be any 'Error' event.
Is there a way to monitor the connection status?
Figured it out. The messagereceived event actually fires when the connection fails. Inside the handler, you can use a try catch block around the data reader operation.
try {
var dataReader = args.getDataReader();
var msg = dataReader.readString(dataReader.unconsumedBufferLength);
} catch (ex) {
var error = Windows.Networking.Sockets.SocketError.getStatus(ex.number);
// do something
}