1 /* -----------------------------------------------------------------------
2 * Copyright (C) 2014 Equinox Software, Inc.
3 * Bill Erickson <berick@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * ----------------------------------------------------------------------- */
17 var WEBSOCKET_URL_PATH = '/osrf-websocket-translator';
18 var WEBSOCKET_PORT_SSL = @WS_PORT@;
20 OpenSRF.WebSocket = function() {
21 this.pending_messages = [];
24 OpenSRF.WebSocket.prototype.connected = function() {
27 this.socket.readyState == this.socket.OPEN
32 * If our global socket is already open, use it. Otherwise, queue the
33 * message for delivery after the socket is open.
35 OpenSRF.WebSocket.prototype.send = function(message) {
38 if (this.connected()) {
39 // this.socket connection is viable. send our message now.
40 this.socket.send(message);
44 // no viable connection. queue our outbound messages for future delivery.
45 this.pending_messages.push(message);
46 console.log('pending count ' + this.pending_messages.length);
48 if (this.socket && this.socket.readyState == this.socket.CONNECTING) {
49 // we are already in the middle of a setup call.
50 // our queued message will be delivered after setup completes.
54 // we have no websocket or an invalid websocket. build a new one.
56 var path = 'wss://' + location.host + ':' +
57 WEBSOCKET_PORT_SSL + WEBSOCKET_URL_PATH;
59 console.debug('connecting websocket to ' + path);
62 this.socket = new WebSocket(path);
64 console.log('Error creating WebSocket for path ' + path + ' : ' + E);
68 this.socket.onopen = function() {
69 console.debug('websocket.onopen()');
70 // deliver any queued messages
72 console.log('pending count ' + self.pending_messages.length);
73 while ( (msg = self.pending_messages.shift()) )
74 self.socket.send(msg);
77 this.socket.onmessage = function(evt) {
78 self.onmessage(evt.data);
82 * Websocket error handler. This type of error indicates a probelem
83 * with the connection. I.e. it's not port-specific.
84 * Broadcast to all ports.
86 this.socket.onerror = function(evt) {
87 var err = "WebSocket Error " + evt + ' : ' + evt.data;
88 self.socket.close(); // connection is no good; reset.
93 * Called when the websocket connection is closed.
95 * Once a websocket is closed, it will be re-opened the next time
96 * a message delivery attempt is made. Clean up and prepare to reconnect.
98 this.socket.onclose = function() {
99 console.debug('closing websocket');
101 if (OpenSRF.onWebSocketClosed)
102 OpenSRF.onWebSocketClosed();