1 /* -----------------------------------------------------------------------
2 * Copyright (C) 2012 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 = 7680;
19 var WEBSOCKET_PORT_SSL = 7682;
22 // Create the websocket and connect to the server
23 // args.onopen is required
24 // if args.default is true, use the default connection
25 OpenSRF.WebSocketConnection = function(args, handlers) {
27 this.handlers = handlers;
29 var secure = (args.ssl || location.protocol == 'https');
30 var path = args.path || WEBSOCKET_URL_PATH;
31 var port = args.port || (secure ? WEBSOCKET_PORT_SSL : WEBSOCKET_PORT);
32 var host = args.host || location.host;
33 var proto = (secure) ? 'wss' : 'ws';
34 this.path = proto + '://' + host + ':' + port + path;
37 OpenSRF.WebSocketConnection.pool[args.name] = this;
40 // global pool of connection objects; name => connection map
41 OpenSRF.WebSocketConnection.pool = {};
43 OpenSRF.WebSocketConnection.defaultConnection = function() {
44 return OpenSRF.WebSocketConnection.pool['default'];
48 * create a new WebSocket. useful for new connections or
49 * applying a new socket to an existing connection (whose
50 * socket was disconnected)
52 OpenSRF.WebSocketConnection.prototype.setupSocket = function() {
55 this.socket = new WebSocket(this.path);
57 throw new Error("WebSocket() not supported in this browser: " + e);
60 this.socket.onopen = this.handlers.onopen;
61 this.socket.onmessage = this.handlers.onmessage;
62 this.socket.onerror = this.handlers.onerror;
63 this.socket.onclose = this.handlers.onclose;
66 /** default onmessage handler: push the message up the opensrf stack */
67 OpenSRF.WebSocketConnection.default_onmessage = function(evt) {
68 //console.log('receiving: ' + evt.data);
69 var msg = JSON2js(evt.data);
71 new OpenSRF.NetMessage(
72 null, null, msg.thread, null, msg.osrf_msg)
76 /** default error handler */
77 OpenSRF.WebSocketConnection.default_onerror = function(evt) {
78 throw new Error("WebSocket Error " + evt + ' : ' + evt.data);
83 OpenSRF.WebSocketConnection.prototype.destroy = function() {
85 delete OpenSRF.WebSocketConnection.pool[this.name];
89 * Creates the request object, but does not connect or send anything
90 * until the first call to send().
92 OpenSRF.WebSocketRequest = function(session, onopen, connectionArgs) {
93 this.session = session;
95 this.setupConnection(connectionArgs || {});
98 OpenSRF.WebSocketRequest.prototype.setupConnection = function(args) {
101 var cname = args.name || 'default';
102 this.wsc = OpenSRF.WebSocketConnection.pool[cname];
104 if (this.wsc) { // we have a WebSocketConnection.
106 switch (this.wsc.socket.readyState) {
108 case this.wsc.socket.CONNECTING:
109 // replace the original onopen handler with a new combined handler
110 var orig_open = this.wsc.socket.onopen;
111 this.wsc.socket.onopen = function() {
117 case this.wsc.socket.OPEN:
118 // user is expecting an onopen event. socket is
119 // already open, so we have to manufacture one.
124 console.log('WebSocket is no longer connecting; reconnecting');
125 this.wsc.setupSocket();
128 } else { // no connection found
130 if (cname == 'default' || args.useDefaultHandlers) { // create the default handle
132 this.wsc = new OpenSRF.WebSocketConnection(
134 onopen : function(evt) {if (self.onopen) self.onopen(self)},
135 onmessage : OpenSRF.WebSocketConnection.default_onmessage,
136 onerror : OpenSRF.WebSocketRequest.default_onerror,
137 onclose : OpenSRF.WebSocketRequest.default_onclose
142 throw new Error("No such WebSocketConnection '" + cname + "'");
148 OpenSRF.WebSocketRequest.prototype.send = function(message) {
150 service : this.session.service,
151 thread : this.session.thread,
152 osrf_msg : [message.serialize()]
155 var json = js2JSON(wrapper);
156 //console.log('sending: ' + json);
158 // drop it on the wire
159 this.wsc.socket.send(json);