-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrest.js
345 lines (305 loc) · 7.22 KB
/
strest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Javascript client for STREST protocol.
*
* Assumes a websocket is available to a server supporting STREST requests.
*
*
*
* @return
*/
function Strest(config) {
this.config = $.extend({
//The url to connect to
url : "Connectionurl",
//called when the connection is closed
onclose : function(event) {
},
//called when the connection is opened
onopen : function(event) {
},
//will attempt to keep the connection live
//will attempt to reconnect if the connection
// is broken. onclose, onopen will still be
// called on reconnect
keepalive : true,
//the endpoint to use for keep alives
//endpoint only needs to return a 200 strest packet
ping : "/ping"
}, config)
//callbacks keyed by transactionId
this.callbacks = {};
this.pingTimer = null
this.reconnectTimer = null
var self = this;
this._onmessage = function(event) {
//console.log(event.data);
var response = new StrestResponse(event.data);
var txnId = response.getHeader('txn.id');
var cb = self.callbacks[txnId];
if (!cb) {
console.log("ERROR: No callback registered for : " + response);
return;
}
if (cb['onmessage']) {
cb.onmessage(response);
}
if ('complete' == response.getHeader('txn.status')) {
if (cb['ontxncomplete']) {
cb.ontxncomplete(txnId);
}
delete self.callbacks[txnId];
}
};
this._onclose = function(event) {
//iterate over all the waiting callbacks and send the error
for (var k in self.callbacks) {
var cb = self.callbacks[k];
if (cb.onerror) {
cb.onerror('Connection closed');
}
}
self.callbacks = {};
if (self.pingTimer) {
clearInterval(self.pingTimer)
self.pingTimer = null
}
if (self.config.onclose) {
self.config.onclose(event);
}
if (self.config.keepalive) {
if (self.reconnectTimer) {
clearInterval(self.reconnectTimer)
}
self.reconnectTimer = setInterval(function() {
console.log("reconnect attempt");
self.connect();
}, 4000)
}
};
this._onopen = function(event) {
//clear the reconnect timer
if (self.reconnectTimer) {
clearInterval(self.reconnectTimer)
}
//start ping timer.
if (self.config.keepalive && self.config.ping) {
//this should never be, but best to be extra safe
if (self.pingTimer) {
clearInterval(self.pingTimer)
self.pingTimer = null
}
self.pingTimer = setInterval(function() {
self.sendRequest({
uri : self.config.ping
},
function(response) {},
function(txnId) {},
function(error) {
//ping error
console.log("Ping error!", error)
self.close()
}
);
}, 20000)
}
if (self.config.onopen) {
self.config.onopen(event);
}
};
this._onerror = function(event) {
self.close();
};
}
/*
* Globals
*
*/
Strest._txnId = 0;
Strest.userAgent = "JsStrest 2.0";
Strest.protocol = "2";
Strest.txnId = function() {
return Strest._txnId++;
};
Strest.prototype.close = function() {
if (this.connected()) {
this.socket.close();
}
};
//connect to the websocket
Strest.prototype.connect = function() {
if (this.connected()) {
console.log("already connected")
return;
}
//initialize the websocket
if (window.WebSocket) {
this.socket = new WebSocket(this.config.url);
this.socket.onmessage = this._onmessage;
this.socket.onopen = this._onopen;
this.socket.onclose = this._onclose;
} else {
alert("Your browser does not support Web Socket.");
}
};
Strest.prototype.connected = function() {
if (!this.socket) {
return false;
}
return this.socket.readyState == WebSocket.OPEN;
};
/**
* Sends a request.
*
*
*
* @param request
* @param message_callback(response)
* @param txn_end_callback(txnId)
* @param error_callback(error)
* @return
*/
Strest.prototype.sendRequest = function(request, message_callback, txn_complete_callback, error_callback) {
if (!this.connected()) {
throw "Not yet connected!";
}
//set up the headers.
if (!(request instanceof StrestRequest)) {
request = new StrestRequest(request);
}
request.setHeaderIfAbsent('method', 'GET');
request.setHeaderIfAbsent('user-agent', Strest.userAgent);
request.setHeaderIfAbsent('txn.id', Strest.txnId());
request.setHeaderIfAbsent('txn.accept', 'multi');
//register the callbacks
this.callbacks[request.getHeader('txn.id')] = {
'onmessage' : message_callback,
'ontxncomplete' : txn_complete_callback,
'onerror' : error_callback
};
var msg = request.toString();
this.socket.send(msg);
return request;
};
function StrestMessage() {
this.strest = {};
this.strest.txn = {};
};
/**
* Set a field in the request. honors the dot operator
* @param key
* @param value
* @return
*/
StrestMessage.prototype.setHeader = function(key, value) {
var keys = key.split('.')
var d = this['strest'];
for (var i = 0, len = keys.length; i < len; i++) {
var k = keys[i];
if (i == keys.length-1) {
d[k] = value;
} else {
if (!d[k]) {
d[k] = {};
}
d = d[k];
}
}
};
StrestMessage.prototype.setHeaderIfAbsent = function(key, value) {
var v = this.getHeader(key);
if (v != null) {
return v;
}
this.setHeader(key, value);
};
StrestMessage.prototype.getHeader = function(key) {
var keys = key.split('.');
var d = this['strest'];
for (var i = 0, len = keys.length; i < len; i++) {
var k = keys[i];
d = d[k];
if (i == keys.length-1) {
return d;
}
}
return null;
};
StrestRequest.prototype = new StrestMessage;
StrestRequest.prototype.constructor = StrestRequest;
/**
* A strest request.
*
* example:
* var request = new StrestRequest({ method : 'GET', uri : '/firehose'})
*
*
*
* @param options
* uri
* method : one of 'GET', 'POST', 'PUT', 'DELETE'
* params : these will get added to the uri
*
*/
function StrestRequest(options) {
StrestMessage.call(this);
// console.log(options);
this.setHeader('method', options['method']);
this.setHeader('v', Strest.protocol);
this.setHeader('user-agent', Strest.userAgent);
this.setHeader('uri', options['uri']);
this.setHeader('params', options['params']);
// console.log(this);
};
StrestRequest.prototype.setUri = function(uri) {
this.setHeader('uri', uri);
};
StrestRequest.prototype.setMethod = function(method) {
this.setHeader('method', method);
};
StrestRequest.prototype.toString = function() {
return JSON.stringify(this);
};
StrestResponse.prototype = new StrestMessage;
StrestResponse.prototype.constructor = StrestResponse;
/**
* A strest response.
*
* @param options
*/
function StrestResponse(msg) {
StrestMessage.call(this);
this.originalText = msg;
var tmp = JSON.parse(msg)
for (key in tmp) {
this[key] = tmp[key]
}
};
StrestResponse.prototype.toString = function() {
return this.originalText;
};
Strest.isString= function(obj) {
if (!obj) {
return false;
}
if (obj instanceof String) {
return true;
}
if (typeof(obj) == 'string') {
return true;
}
return false;
};
Strest.trim = function(str) {
if (!Strest.isString(str)) {
return str;
}
// http://forum.jquery.com/topic/faster-jquery-trimjavascript to
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
};