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') {
if (data.action == 'message') {
// pass all inbound message up the opensrf stack
+ OpenSRF.sharedWebsocketConnected = true;
var msg;
try {
msg = JSON2js(data.message);
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);
+ }
}
});
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.
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;
this.socket.onclose = function() {
console.debug('closing websocket');
self.socket = null;
+ if (OpenSRF.onWebSocketClosed)
+ OpenSRF.onWebSocketClosed();
}
}
var msg;
while ( (msg = pending_ws_messages.shift()) )
websocket.send(msg);
+
}
websocket.onmessage = function(evt) {
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.
}
console.debug('closing websocket');
websocket = null;
thread_port_map = {};
+ broadcast({action : 'event', type : 'onclose'});
}
}