From d9728940d28dbe704ad090a2faf102703814518c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 30 Jul 2015 10:13:35 -0400 Subject: [PATCH 1/3] Fix occasional error with sending while readyState !== 1 Under certain conditions, noVNC would attempt to flush the web socket while it was disconnected, before the disconnected state was picked up. This casues noVNC to crash ungracefully and the parent window is not notified - leading to no chance at recovery without a page refresh. --- include/websock.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/websock.js b/include/websock.js index cc82e5a2..8e0660e7 100644 --- a/include/websock.js +++ b/include/websock.js @@ -175,7 +175,7 @@ function Websock() { } if (this._websocket.bufferedAmount < this.maxBufferedAmount) { - if (this._sQ.length > 0) { + if (this._sQ.length > 0 && this._websocket.readyState === 1) { this._websocket.send(this._encode_message()); this._sQ = []; } From bdda61fc738ed4949a60509263d5389ef1529792 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 30 Jul 2015 10:32:25 -0400 Subject: [PATCH 2/3] Fix failing test It only makes sense to send data on a websocket if the readyState is equal to 1. --- tests/test.websock.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test.websock.js b/tests/test.websock.js index 7d242d3e..0918d8c3 100644 --- a/tests/test.websock.js +++ b/tests/test.websock.js @@ -180,6 +180,7 @@ describe('Websock', function() { it('should actually send on the websocket if the websocket does not have too much buffered', function () { sock.maxBufferedAmount = 10; sock._websocket.bufferedAmount = 8; + sock._websocket.readyState = 1; sock._sQ = [1, 2, 3]; var encoded = sock._encode_message(); From 5122a8a4ce7cb4958c62ef1c84ce4220f3a3ab0d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 30 Jul 2015 11:37:00 -0400 Subject: [PATCH 3/3] Check readyState against constant WebSocket.OPEN --- include/websock.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/websock.js b/include/websock.js index 8e0660e7..c5509047 100644 --- a/include/websock.js +++ b/include/websock.js @@ -175,7 +175,7 @@ function Websock() { } if (this._websocket.bufferedAmount < this.maxBufferedAmount) { - if (this._sQ.length > 0 && this._websocket.readyState === 1) { + if (this._sQ.length > 0 && this._websocket.readyState === WebSocket.OPEN) { this._websocket.send(this._encode_message()); this._sQ = []; }