]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf_ws.js
LP#1268619: OpenSRF JS websockets plugin
[OpenSRF.git] / src / javascript / opensrf_ws.js
1 /* -----------------------------------------------------------------------
2  * Copyright (C) 2012  Equinox Software, Inc.
3  * Bill Erickson <berick@esilibrary.com>
4  *  
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.
9  * 
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  * ----------------------------------------------------------------------- */
15
16 var WS_PATH = '/osrf-websocket';
17
18 /**
19  * onopen is required. no data can be sent 
20  * until the async connection dance completes.
21  */
22 OpenSRF.WSRequest = function(session, args, onopen) {
23     this.session = session;
24     this.args = args;
25
26     var proto = location.protocol == 'https' ? 'wss' : 'ws';
27
28     var path = proto + '://' + location.host + 
29         WS_PATH + '?service=' + this.session.service;
30
31     try {
32         this.ws = new WebSocket(path);
33     } catch(e) {
34         throw new Error("WebSocket() not supported in this browser " + e);
35     }
36
37     var self = this;
38
39     this.ws.onopen = function(evt) {
40         onopen(self);
41     }
42
43     this.ws.onmessage = function(evt) {
44         self.core_handler(evt.data);
45     }
46
47     this.ws.onerror = function(evt) {
48         self.transport_error_handler(evt.data);
49     }
50
51     this.ws.onclose = function(evt) {
52     }
53 };
54
55 OpenSRF.WSRequest.prototype.send = function(message) {
56     //console.log('sending: ' + js2JSON([message.serialize()]));
57     this.last_message = message;
58     this.ws.send(js2JSON([message.serialize()]));
59     return this;
60 };
61
62 OpenSRF.WSRequest.prototype.close = function() {
63     try { this.ws.close(); } catch(e) {}
64 }
65
66 OpenSRF.WSRequest.prototype.core_handler = function(json) {
67     //console.log('received: ' + json);
68
69     OpenSRF.Stack.push(
70         new OpenSRF.NetMessage(null, null, '', json),
71         {
72             onresponse : this.args.onresponse,
73             oncomplete : this.args.oncomplete,
74             onerror : this.args.onerror,
75             onmethoderror : this.method_error_handler()
76         },
77         this.args.session
78     );
79 };
80
81
82 OpenSRF.WSRequest.prototype.method_error_handler = function() {
83     var self = this;
84     return function(req, status, status_text) {
85         if(self.args.onmethoderror) 
86             self.args.onmethoderror(req, status, status_text);
87
88         if(self.args.onerror)  {
89             self.args.onerror(
90                 self.last_message, self.session.service, '');
91         }
92     };
93 };
94
95 OpenSRF.WSRequest.prototype.transport_error_handler = function(msg) {
96     if(this.args.ontransporterror) {
97         this.args.ontransporterror(msg);
98     }
99     if(this.args.onerror) {
100         this.args.onerror(msg, this.session.service, '');
101     }
102 };
103
104