LP#1268619: websockets: detect connectedness of JS default sockets
authorBill Erickson <berick@esilibrary.com>
Thu, 10 Apr 2014 12:50:13 +0000 (08:50 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Tue, 19 Aug 2014 22:54:47 +0000 (15:54 -0700)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>

src/javascript/opensrf.js
src/javascript/opensrf_ws.js
src/javascript/opensrf_ws_shared.js

index 997b33f..f9b0895 100644 (file)
@@ -248,6 +248,13 @@ OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
     new OpenSRF.XHRequest(osrf_msg, args).send();
 };
 
+OpenSRF.websocketConnected = function() {
+    return OpenSRF.sharedWebsocketConnected || (
+        OpenSRF.websocketConnection && 
+        OpenSRF.websocketConnection.connected()
+    );
+}
+
 OpenSRF.Session.prototype.send_ws = function(osrf_msg) {
 
     if (typeof SharedWorker == 'function') {
@@ -303,6 +310,7 @@ OpenSRF.Session.setup_shared_ws = function() {
         if (data.action == 'message') {
             // pass all inbound message up the opensrf stack
 
+            OpenSRF.sharedWebsocketConnected = true;
             var msg;
             try {
                 msg = JSON2js(data.message);
@@ -319,8 +327,16 @@ OpenSRF.Session.setup_shared_ws = function() {
             return;
         }
 
-        if (data.action == 'error') {
-            throw new Error(data.message);
+
+        if (data.action == 'event') {
+            console.debug('event type is ' + data.type);
+            if (data.type.match(/onclose|onerror/)) {
+                OpenSRF.sharedWebsocketConnected = false;
+                if (OpenSRF.onWebSocketClosed)
+                    OpenSRF.onWebSocketClosed();
+                if (data.type.match(/onerror/)) 
+                    throw new Error(data.message);
+            }
         }
     });
 
index 352bd2a..74fc40e 100644 (file)
@@ -21,6 +21,13 @@ OpenSRF.WebSocket = function() {
     this.pending_messages = [];
 }
 
+OpenSRF.WebSocket.prototype.connected = function() {
+    return (
+        this.socket && 
+        this.socket.readyState == this.socket.OPEN
+    );
+}
+
 /**
  * If our global socket is already open, use it.  Otherwise, queue the 
  * message for delivery after the socket is open.
@@ -28,7 +35,7 @@ OpenSRF.WebSocket = function() {
 OpenSRF.WebSocket.prototype.send = function(message) {
     var self = this;
 
-    if (this.socket && this.socket.readyState == this.socket.OPEN) {
+    if (this.connected()) {
         // this.socket connection is viable.  send our message now.
         this.socket.send(message);
         return;
@@ -91,5 +98,7 @@ OpenSRF.WebSocket.prototype.send = function(message) {
     this.socket.onclose = function() {
         console.debug('closing websocket');
         self.socket = null;
+        if (OpenSRF.onWebSocketClosed)
+            OpenSRF.onWebSocketClosed();
     }
 }
index ff0b586..36c5baf 100644 (file)
@@ -128,6 +128,7 @@ function send_to_websocket(message) {
         var msg;
         while ( (msg = pending_ws_messages.shift()) )
             websocket.send(msg);
+
     }
 
     websocket.onmessage = function(evt) {
@@ -189,7 +190,7 @@ function send_to_websocket(message) {
     websocket.onerror = function(evt) {
         var err = "WebSocket Error " + evt;
         console.error(err);
-        broadcast({action : 'error', message : err});
+        broadcast({action : 'event', type : 'onerror', message : err});
         websocket.close(); // connection is no good; reset.
     }
 
@@ -203,6 +204,7 @@ function send_to_websocket(message) {
         console.debug('closing websocket');
         websocket = null;
         thread_port_map = {};
+        broadcast({action : 'event', type : 'onclose'});
     }
 }