]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Provide readyState for Websock objects
authorPierre Ossman <ossman@cendio.se>
Sun, 18 Apr 2021 10:52:16 +0000 (12:52 +0200)
committerPierre Ossman <ossman@cendio.se>
Sun, 18 Apr 2021 12:26:05 +0000 (14:26 +0200)
It mainly reports the state of the underlying object in consistent
manner.

core/websock.js
tests/test.websock.js

index 89ccdd94d88c572ff5692b4455b5bd4a1d2c347d..52e27be51c8db3bac656d6192963c3d25cb6df1b 100644 (file)
@@ -71,6 +71,29 @@ export default class Websock {
     }
 
     // Getters and Setters
+
+    get readyState() {
+        let subState;
+
+        if (this._websocket === null) {
+            return "unused";
+        }
+
+        subState = this._websocket.readyState;
+
+        if (ReadyStates.CONNECTING.includes(subState)) {
+            return "connecting";
+        } else if (ReadyStates.OPEN.includes(subState)) {
+            return "open";
+        } else if (ReadyStates.CLOSING.includes(subState)) {
+            return "closing";
+        } else if (ReadyStates.CLOSED.includes(subState)) {
+            return "closed";
+        }
+
+        return "unknown";
+    }
+
     get sQ() {
         return this._sQ;
     }
@@ -168,7 +191,7 @@ export default class Websock {
     // Send Queue
 
     flush() {
-        if (this._sQlen > 0 && ReadyStates.OPEN.indexOf(this._websocket.readyState) >= 0) {
+        if (this._sQlen > 0 && this.readyState === 'open') {
             this._websocket.send(this._encodeMessage());
             this._sQlen = 0;
         }
@@ -234,9 +257,7 @@ export default class Websock {
             Log.Debug("<< WebSock.onopen");
         };
 
-        // If the readyState cannot be found this defaults to assuming it's not open.
-        const isOpen = ReadyStates.OPEN.indexOf(this._websocket.readyState) >= 0;
-        if (isOpen) {
+        if (this.readyState === 'open') {
             onOpen();
         } else {
             this._websocket.onopen = onOpen;
@@ -257,8 +278,8 @@ export default class Websock {
 
     close() {
         if (this._websocket) {
-            if (ReadyStates.CONNECTING.indexOf(this._websocket.readyState) >= 0 ||
-                ReadyStates.OPEN.indexOf(this._websocket.readyState) >= 0) {
+            if (this.readyState === 'connecting' ||
+                this.readyState === 'open') {
                 Log.Info("Closing WebSocket connection");
                 this._websocket.close();
             }
index 196a3003d8543df1422cffb68fdae096cb122e0b..1c2aa13c74763c863175766ceb16d68296e85107 100644 (file)
@@ -363,6 +363,93 @@ describe('Websock', function () {
             });
         });
 
+        describe('ready state', function () {
+            it('should be "unused" after construction', function () {
+                let sock = new Websock();
+                expect(sock.readyState).to.equal('unused');
+            });
+
+            it('should be "connecting" if WebSocket is connecting', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = WebSocket.CONNECTING;
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('connecting');
+            });
+
+            it('should be "open" if WebSocket is open', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = WebSocket.OPEN;
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('open');
+            });
+
+            it('should be "closing" if WebSocket is closing', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = WebSocket.CLOSING;
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('closing');
+            });
+
+            it('should be "closed" if WebSocket is closed', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = WebSocket.CLOSED;
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('closed');
+            });
+
+            it('should be "unknown" if WebSocket state is unknown', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 666;
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('unknown');
+            });
+
+            it('should be "connecting" if RTCDataChannel is connecting', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 'connecting';
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('connecting');
+            });
+
+            it('should be "open" if RTCDataChannel is open', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 'open';
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('open');
+            });
+
+            it('should be "closing" if RTCDataChannel is closing', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 'closing';
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('closing');
+            });
+
+            it('should be "closed" if RTCDataChannel is closed', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 'closed';
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('closed');
+            });
+
+            it('should be "unknown" if RTCDataChannel state is unknown', function () {
+                let sock = new Websock();
+                let ws = new FakeWebSocket();
+                ws.readyState = 'foobar';
+                sock.attach(ws);
+                expect(sock.readyState).to.equal('unknown');
+            });
+        });
+
         after(function () {
             // eslint-disable-next-line no-global-assign
             WebSocket = oldWS;