ucservice 1.8.1 → 1.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/cometd.js DELETED
@@ -1,3286 +0,0 @@
1
- /*
2
- * Copyright (c) 2008-2016 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- /* CometD Version 3.1.0 */
18
-
19
- (function(root, factory){
20
- if (typeof exports === 'object') {
21
- // CommonJS.
22
- module.exports = factory();
23
- } else if (typeof define === 'function' && define.amd) {
24
- // AMD.
25
- define([], factory);
26
- } else {
27
- // Globals.
28
- root.org = root.org || {};
29
- root.org.cometd = factory();
30
- }
31
- }(this, function() {
32
- /**
33
- * Utility functions.
34
- */
35
- var Utils = {
36
- isString: function(value) {
37
- if (value === undefined || value === null) {
38
- return false;
39
- }
40
- return typeof value === 'string' || value instanceof String;
41
- },
42
- isArray: function(value) {
43
- if (value === undefined || value === null) {
44
- return false;
45
- }
46
- return value instanceof Array;
47
- },
48
- /**
49
- * Returns whether the given element is contained into the given array.
50
- * @param element the element to check presence for
51
- * @param array the array to check for the element presence
52
- * @return the index of the element, if present, or a negative index if the element is not present
53
- */
54
- inArray: function(element, array) {
55
- for (var i = 0; i < array.length; ++i) {
56
- if (element === array[i]) {
57
- return i;
58
- }
59
- }
60
- return -1;
61
- },
62
- setTimeout: function(cometd, funktion, delay) {
63
- return window.setTimeout(function() {
64
- try {
65
- cometd._debug('Invoking timed function', funktion);
66
- funktion();
67
- } catch (x) {
68
- cometd._debug('Exception invoking timed function', funktion, x);
69
- }
70
- }, delay);
71
- },
72
- clearTimeout: function(timeoutHandle) {
73
- window.clearTimeout(timeoutHandle);
74
- }
75
- };
76
-
77
-
78
- /**
79
- * A registry for transports used by the CometD object.
80
- */
81
- var TransportRegistry = function() {
82
- var _types = [];
83
- var _transports = {};
84
-
85
- this.getTransportTypes = function() {
86
- return _types.slice(0);
87
- };
88
-
89
- this.findTransportTypes = function(version, crossDomain, url) {
90
- var result = [];
91
- for (var i = 0; i < _types.length; ++i) {
92
- var type = _types[i];
93
- if (_transports[type].accept(version, crossDomain, url) === true) {
94
- result.push(type);
95
- }
96
- }
97
- return result;
98
- };
99
-
100
- this.negotiateTransport = function(types, version, crossDomain, url) {
101
- for (var i = 0; i < _types.length; ++i) {
102
- var type = _types[i];
103
- for (var j = 0; j < types.length; ++j) {
104
- if (type === types[j]) {
105
- var transport = _transports[type];
106
- if (transport.accept(version, crossDomain, url) === true) {
107
- return transport;
108
- }
109
- }
110
- }
111
- }
112
- return null;
113
- };
114
-
115
- this.add = function(type, transport, index) {
116
- var existing = false;
117
- for (var i = 0; i < _types.length; ++i) {
118
- if (_types[i] === type) {
119
- existing = true;
120
- break;
121
- }
122
- }
123
-
124
- if (!existing) {
125
- if (typeof index !== 'number') {
126
- _types.push(type);
127
- } else {
128
- _types.splice(index, 0, type);
129
- }
130
- _transports[type] = transport;
131
- }
132
-
133
- return !existing;
134
- };
135
-
136
- this.find = function(type) {
137
- for (var i = 0; i < _types.length; ++i) {
138
- if (_types[i] === type) {
139
- return _transports[type];
140
- }
141
- }
142
- return null;
143
- };
144
-
145
- this.remove = function(type) {
146
- for (var i = 0; i < _types.length; ++i) {
147
- if (_types[i] === type) {
148
- _types.splice(i, 1);
149
- var transport = _transports[type];
150
- delete _transports[type];
151
- return transport;
152
- }
153
- }
154
- return null;
155
- };
156
-
157
- this.clear = function() {
158
- _types = [];
159
- _transports = {};
160
- };
161
-
162
- this.reset = function(init) {
163
- for (var i = 0; i < _types.length; ++i) {
164
- _transports[_types[i]].reset(init);
165
- }
166
- };
167
- };
168
-
169
-
170
- /**
171
- * Base object with the common functionality for transports.
172
- */
173
- var Transport = function() {
174
- var _type;
175
- var _cometd;
176
- var _url;
177
-
178
- /**
179
- * Function invoked just after a transport has been successfully registered.
180
- * @param type the type of transport (for example 'long-polling')
181
- * @param cometd the cometd object this transport has been registered to
182
- * @see #unregistered()
183
- */
184
- this.registered = function(type, cometd) {
185
- _type = type;
186
- _cometd = cometd;
187
- };
188
-
189
- /**
190
- * Function invoked just after a transport has been successfully unregistered.
191
- * @see #registered(type, cometd)
192
- */
193
- this.unregistered = function() {
194
- _type = null;
195
- _cometd = null;
196
- };
197
-
198
- this._debug = function() {
199
- _cometd._debug.apply(_cometd, arguments);
200
- };
201
-
202
- this._mixin = function() {
203
- return _cometd._mixin.apply(_cometd, arguments);
204
- };
205
-
206
- this.getConfiguration = function() {
207
- return _cometd.getConfiguration();
208
- };
209
-
210
- this.getAdvice = function() {
211
- return _cometd.getAdvice();
212
- };
213
-
214
- this.setTimeout = function(funktion, delay) {
215
- return Utils.setTimeout(_cometd, funktion, delay);
216
- };
217
-
218
- this.clearTimeout = function(handle) {
219
- Utils.clearTimeout(handle);
220
- };
221
-
222
- /**
223
- * Converts the given response into an array of bayeux messages
224
- * @param response the response to convert
225
- * @return an array of bayeux messages obtained by converting the response
226
- */
227
- this.convertToMessages = function(response) {
228
- if (Utils.isString(response)) {
229
- try {
230
- return JSON.parse(response);
231
- } catch (x) {
232
- this._debug('Could not convert to JSON the following string', '"' + response + '"');
233
- throw x;
234
- }
235
- }
236
- if (Utils.isArray(response)) {
237
- return response;
238
- }
239
- if (response === undefined || response === null) {
240
- return [];
241
- }
242
- if (response instanceof Object) {
243
- return [response];
244
- }
245
- throw 'Conversion Error ' + response + ', typeof ' + (typeof response);
246
- };
247
-
248
- /**
249
- * Returns whether this transport can work for the given version and cross domain communication case.
250
- * @param version a string indicating the transport version
251
- * @param crossDomain a boolean indicating whether the communication is cross domain
252
- * @param url the URL to connect to
253
- * @return true if this transport can work for the given version and cross domain communication case,
254
- * false otherwise
255
- */
256
- this.accept = function(version, crossDomain, url) {
257
- throw 'Abstract';
258
- };
259
-
260
- /**
261
- * Returns the type of this transport.
262
- * @see #registered(type, cometd)
263
- */
264
- this.getType = function() {
265
- return _type;
266
- };
267
-
268
- this.getURL = function() {
269
- return _url;
270
- };
271
-
272
- this.setURL = function(url) {
273
- _url = url;
274
- };
275
-
276
- this.send = function(envelope, metaConnect) {
277
- throw 'Abstract';
278
- };
279
-
280
- this.reset = function(init) {
281
- this._debug('Transport', _type, 'reset', init ? 'initial' : 'retry');
282
- };
283
-
284
- this.abort = function() {
285
- this._debug('Transport', _type, 'aborted');
286
- };
287
-
288
- this.toString = function() {
289
- return this.getType();
290
- };
291
- };
292
-
293
- Transport.derive = function(baseObject) {
294
- function F() {
295
- }
296
-
297
- F.prototype = baseObject;
298
- return new F();
299
- };
300
-
301
-
302
- /**
303
- * Base object with the common functionality for transports based on requests.
304
- * The key responsibility is to allow at most 2 outstanding requests to the server,
305
- * to avoid that requests are sent behind a long poll.
306
- * To achieve this, we have one reserved request for the long poll, and all other
307
- * requests are serialized one after the other.
308
- */
309
- var RequestTransport = function() {
310
- var _super = new Transport();
311
- var _self = Transport.derive(_super);
312
- var _requestIds = 0;
313
- var _metaConnectRequest = null;
314
- var _requests = [];
315
- var _envelopes = [];
316
-
317
- function _coalesceEnvelopes(envelope) {
318
- while (_envelopes.length > 0) {
319
- var envelopeAndRequest = _envelopes[0];
320
- var newEnvelope = envelopeAndRequest[0];
321
- var newRequest = envelopeAndRequest[1];
322
- if (newEnvelope.url === envelope.url &&
323
- newEnvelope.sync === envelope.sync) {
324
- _envelopes.shift();
325
- envelope.messages = envelope.messages.concat(newEnvelope.messages);
326
- this._debug('Coalesced', newEnvelope.messages.length, 'messages from request', newRequest.id);
327
- continue;
328
- }
329
- break;
330
- }
331
- }
332
-
333
- function _transportSend(envelope, request) {
334
- this.transportSend(envelope, request);
335
- request.expired = false;
336
-
337
- if (!envelope.sync) {
338
- var maxDelay = this.getConfiguration().maxNetworkDelay;
339
- var delay = maxDelay;
340
- if (request.metaConnect === true) {
341
- delay += this.getAdvice().timeout;
342
- }
343
-
344
- this._debug('Transport', this.getType(), 'waiting at most', delay, 'ms for the response, maxNetworkDelay', maxDelay);
345
-
346
- var self = this;
347
- request.timeout = this.setTimeout(function() {
348
- request.expired = true;
349
- var errorMessage = 'Request ' + request.id + ' of transport ' + self.getType() + ' exceeded ' + delay + ' ms max network delay';
350
- var failure = {
351
- reason: errorMessage
352
- };
353
- var xhr = request.xhr;
354
- failure.httpCode = self.xhrStatus(xhr);
355
- self.abortXHR(xhr);
356
- self._debug(errorMessage);
357
- self.complete(request, false, request.metaConnect);
358
- envelope.onFailure(xhr, envelope.messages, failure);
359
- }, delay);
360
- }
361
- }
362
-
363
- function _queueSend(envelope) {
364
- var requestId = ++_requestIds;
365
- var request = {
366
- id: requestId,
367
- metaConnect: false,
368
- envelope: envelope
369
- };
370
-
371
- // Consider the metaConnect requests which should always be present
372
- if (_requests.length < this.getConfiguration().maxConnections - 1) {
373
- _requests.push(request);
374
- _transportSend.call(this, envelope, request);
375
- } else {
376
- this._debug('Transport', this.getType(), 'queueing request', requestId, 'envelope', envelope);
377
- _envelopes.push([envelope, request]);
378
- }
379
- }
380
-
381
- function _metaConnectComplete(request) {
382
- var requestId = request.id;
383
- this._debug('Transport', this.getType(), 'metaConnect complete, request', requestId);
384
- if (_metaConnectRequest !== null && _metaConnectRequest.id !== requestId) {
385
- throw 'Longpoll request mismatch, completing request ' + requestId;
386
- }
387
-
388
- // Reset metaConnect request
389
- _metaConnectRequest = null;
390
- }
391
-
392
- function _complete(request, success) {
393
- var index = Utils.inArray(request, _requests);
394
- // The index can be negative if the request has been aborted
395
- if (index >= 0) {
396
- _requests.splice(index, 1);
397
- }
398
-
399
- if (_envelopes.length > 0) {
400
- var envelopeAndRequest = _envelopes.shift();
401
- var nextEnvelope = envelopeAndRequest[0];
402
- var nextRequest = envelopeAndRequest[1];
403
- this._debug('Transport dequeued request', nextRequest.id);
404
- if (success) {
405
- if (this.getConfiguration().autoBatch) {
406
- _coalesceEnvelopes.call(this, nextEnvelope);
407
- }
408
- _queueSend.call(this, nextEnvelope);
409
- this._debug('Transport completed request', request.id, nextEnvelope);
410
- } else {
411
- // Keep the semantic of calling response callbacks asynchronously after the request
412
- var self = this;
413
- this.setTimeout(function() {
414
- self.complete(nextRequest, false, nextRequest.metaConnect);
415
- var failure = {
416
- reason: 'Previous request failed'
417
- };
418
- var xhr = nextRequest.xhr;
419
- failure.httpCode = self.xhrStatus(xhr);
420
- nextEnvelope.onFailure(xhr, nextEnvelope.messages, failure);
421
- }, 0);
422
- }
423
- }
424
- }
425
-
426
- _self.complete = function(request, success, metaConnect) {
427
- if (metaConnect) {
428
- _metaConnectComplete.call(this, request);
429
- } else {
430
- _complete.call(this, request, success);
431
- }
432
- };
433
-
434
- /**
435
- * Performs the actual send depending on the transport type details.
436
- * @param envelope the envelope to send
437
- * @param request the request information
438
- */
439
- _self.transportSend = function(envelope, request) {
440
- throw 'Abstract';
441
- };
442
-
443
- _self.transportSuccess = function(envelope, request, responses) {
444
- if (!request.expired) {
445
- this.clearTimeout(request.timeout);
446
- this.complete(request, true, request.metaConnect);
447
- if (responses && responses.length > 0) {
448
- envelope.onSuccess(responses);
449
- } else {
450
- envelope.onFailure(request.xhr, envelope.messages, {
451
- httpCode: 204
452
- });
453
- }
454
- }
455
- };
456
-
457
- _self.transportFailure = function(envelope, request, failure) {
458
- if (!request.expired) {
459
- this.clearTimeout(request.timeout);
460
- this.complete(request, false, request.metaConnect);
461
- envelope.onFailure(request.xhr, envelope.messages, failure);
462
- }
463
- };
464
-
465
- function _metaConnectSend(envelope) {
466
- if (_metaConnectRequest !== null) {
467
- throw 'Concurrent metaConnect requests not allowed, request id=' + _metaConnectRequest.id + ' not yet completed';
468
- }
469
-
470
- var requestId = ++_requestIds;
471
- this._debug('Transport', this.getType(), 'metaConnect send, request', requestId, 'envelope', envelope);
472
- var request = {
473
- id: requestId,
474
- metaConnect: true,
475
- envelope: envelope
476
- };
477
- _transportSend.call(this, envelope, request);
478
- _metaConnectRequest = request;
479
- }
480
-
481
- _self.send = function(envelope, metaConnect) {
482
- if (metaConnect) {
483
- _metaConnectSend.call(this, envelope);
484
- } else {
485
- _queueSend.call(this, envelope);
486
- }
487
- };
488
-
489
- _self.abort = function() {
490
- _super.abort();
491
- for (var i = 0; i < _requests.length; ++i) {
492
- var request = _requests[i];
493
- if (request) {
494
- this._debug('Aborting request', request);
495
- if (!this.abortXHR(request.xhr)) {
496
- this.transportFailure(request.envelope, request, {reason: 'abort'});
497
- }
498
- }
499
- }
500
- var metaConnectRequest = _metaConnectRequest;
501
- if (metaConnectRequest) {
502
- this._debug('Aborting metaConnect request', metaConnectRequest);
503
- if (!this.abortXHR(metaConnectRequest.xhr)) {
504
- this.transportFailure(metaConnectRequest.envelope, metaConnectRequest, {reason: 'abort'});
505
- }
506
- }
507
- this.reset(true);
508
- };
509
-
510
- _self.reset = function(init) {
511
- _super.reset(init);
512
- _metaConnectRequest = null;
513
- _requests = [];
514
- _envelopes = [];
515
- };
516
-
517
- _self.abortXHR = function(xhr) {
518
- if (xhr) {
519
- try {
520
- var state = xhr.readyState;
521
- xhr.abort();
522
- return state !== XMLHttpRequest.UNSENT;
523
- } catch (x) {
524
- this._debug(x);
525
- }
526
- }
527
- return false;
528
- };
529
-
530
- _self.xhrStatus = function(xhr) {
531
- if (xhr) {
532
- try {
533
- return xhr.status;
534
- } catch (x) {
535
- this._debug(x);
536
- }
537
- }
538
- return -1;
539
- };
540
-
541
- return _self;
542
- };
543
-
544
-
545
- var LongPollingTransport = function() {
546
- var _super = new RequestTransport();
547
- var _self = Transport.derive(_super);
548
- // By default, support cross domain
549
- var _supportsCrossDomain = true;
550
-
551
- _self.accept = function(version, crossDomain, url) {
552
- return _supportsCrossDomain || !crossDomain;
553
- };
554
-
555
- _self.xhrSend = function(packet) {
556
- var xhr = new XMLHttpRequest();
557
- xhr.withCredentials = true;
558
- xhr.open('POST', packet.url, packet.sync !== true);
559
- var headers = packet.headers;
560
- if (headers) {
561
- for (var headerName in headers) {
562
- if (headers.hasOwnProperty(headerName)) {
563
- xhr.setRequestHeader(headerName, headers[headerName]);
564
- }
565
- }
566
- }
567
- xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
568
- xhr.onload = function() {
569
- if (xhr.status === 200) {
570
- packet.onSuccess(xhr.responseText);
571
- } else {
572
- packet.onError(xhr.statusText);
573
- }
574
- };
575
- xhr.onerror = function() {
576
- packet.onError(xhr.statusText);
577
- };
578
- xhr.send(packet.body);
579
- return xhr;
580
- };
581
-
582
- _self.transportSend = function(envelope, request) {
583
- this._debug('Transport', this.getType(), 'sending request', request.id, 'envelope', envelope);
584
-
585
- var self = this;
586
- try {
587
- var sameStack = true;
588
- request.xhr = this.xhrSend({
589
- transport: this,
590
- url: envelope.url,
591
- sync: envelope.sync,
592
- headers: this.getConfiguration().requestHeaders,
593
- body: JSON.stringify(envelope.messages),
594
- onSuccess: function(response) {
595
- self._debug('Transport', self.getType(), 'received response', response);
596
- var success = false;
597
- try {
598
- var received = self.convertToMessages(response);
599
- if (received.length === 0) {
600
- _supportsCrossDomain = false;
601
- self.transportFailure(envelope, request, {
602
- httpCode: 204
603
- });
604
- } else {
605
- success = true;
606
- self.transportSuccess(envelope, request, received);
607
- }
608
- } catch (x) {
609
- self._debug(x);
610
- if (!success) {
611
- _supportsCrossDomain = false;
612
- var failure = {
613
- exception: x
614
- };
615
- failure.httpCode = self.xhrStatus(request.xhr);
616
- self.transportFailure(envelope, request, failure);
617
- }
618
- }
619
- },
620
- onError: function(reason, exception) {
621
- self._debug('Transport', self.getType(), 'received error', reason, exception);
622
- _supportsCrossDomain = false;
623
- var failure = {
624
- reason: reason,
625
- exception: exception
626
- };
627
- failure.httpCode = self.xhrStatus(request.xhr);
628
- if (sameStack) {
629
- // Keep the semantic of calling response callbacks asynchronously after the request
630
- self.setTimeout(function() {
631
- self.transportFailure(envelope, request, failure);
632
- }, 0);
633
- } else {
634
- self.transportFailure(envelope, request, failure);
635
- }
636
- }
637
- });
638
- sameStack = false;
639
- } catch (x) {
640
- _supportsCrossDomain = false;
641
- // Keep the semantic of calling response callbacks asynchronously after the request
642
- this.setTimeout(function() {
643
- self.transportFailure(envelope, request, {
644
- exception: x
645
- });
646
- }, 0);
647
- }
648
- };
649
-
650
- _self.reset = function(init) {
651
- _super.reset(init);
652
- _supportsCrossDomain = true;
653
- };
654
-
655
- return _self;
656
- };
657
-
658
-
659
- var CallbackPollingTransport = function() {
660
- var _super = new RequestTransport();
661
- var _self = Transport.derive(_super);
662
- var jsonp = 0;
663
-
664
- _self.accept = function(version, crossDomain, url) {
665
- return true;
666
- };
667
-
668
- _self.jsonpSend = function(packet) {
669
- var head = document.getElementsByTagName('head')[0];
670
- var script = document.createElement('script');
671
-
672
- var callbackName = '_cometd_jsonp_' + jsonp++;
673
- window[callbackName] = function(responseText) {
674
- head.removeChild(script);
675
- delete window[callbackName];
676
- packet.onSuccess(responseText);
677
- };
678
-
679
- var url = packet.url;
680
- url += url.indexOf('?') < 0 ? '?' : '&';
681
- url += 'jsonp=' + callbackName;
682
- url += '&message=' + packet.body;
683
- script.src = url;
684
- script.async = packet.sync !== true;
685
- script.type = 'application/javascript';
686
- script.onerror = function(e) {
687
- packet.onError('jsonp ' + e.type);
688
- };
689
- head.appendChild(script);
690
- };
691
-
692
- function _failTransportFn(envelope, request, x) {
693
- var self = this;
694
- return function() {
695
- self.transportFailure(envelope, request, 'error', x);
696
- };
697
- }
698
-
699
- _self.transportSend = function(envelope, request) {
700
- var self = this;
701
-
702
- // Microsoft Internet Explorer has a 2083 URL max length
703
- // We must ensure that we stay within that length
704
- var start = 0;
705
- var length = envelope.messages.length;
706
- var lengths = [];
707
- while (length > 0) {
708
- // Encode the messages because all brackets, quotes, commas, colons, etc
709
- // present in the JSON will be URL encoded, taking many more characters
710
- var json = JSON.stringify(envelope.messages.slice(start, start + length));
711
- var urlLength = envelope.url.length + encodeURI(json).length;
712
-
713
- var maxLength = this.getConfiguration().maxURILength;
714
- if (urlLength > maxLength) {
715
- if (length === 1) {
716
- var x = 'Bayeux message too big (' + urlLength + ' bytes, max is ' + maxLength + ') ' +
717
- 'for transport ' + this.getType();
718
- // Keep the semantic of calling response callbacks asynchronously after the request
719
- this.setTimeout(_failTransportFn.call(this, envelope, request, x), 0);
720
- return;
721
- }
722
-
723
- --length;
724
- continue;
725
- }
726
-
727
- lengths.push(length);
728
- start += length;
729
- length = envelope.messages.length - start;
730
- }
731
-
732
- // Here we are sure that the messages can be sent within the URL limit
733
-
734
- var envelopeToSend = envelope;
735
- if (lengths.length > 1) {
736
- var begin = 0;
737
- var end = lengths[0];
738
- this._debug('Transport', this.getType(), 'split', envelope.messages.length, 'messages into', lengths.join(' + '));
739
- envelopeToSend = this._mixin(false, {}, envelope);
740
- envelopeToSend.messages = envelope.messages.slice(begin, end);
741
- envelopeToSend.onSuccess = envelope.onSuccess;
742
- envelopeToSend.onFailure = envelope.onFailure;
743
-
744
- for (var i = 1; i < lengths.length; ++i) {
745
- var nextEnvelope = this._mixin(false, {}, envelope);
746
- begin = end;
747
- end += lengths[i];
748
- nextEnvelope.messages = envelope.messages.slice(begin, end);
749
- nextEnvelope.onSuccess = envelope.onSuccess;
750
- nextEnvelope.onFailure = envelope.onFailure;
751
- this.send(nextEnvelope, request.metaConnect);
752
- }
753
- }
754
-
755
- this._debug('Transport', this.getType(), 'sending request', request.id, 'envelope', envelopeToSend);
756
-
757
- try {
758
- var sameStack = true;
759
- this.jsonpSend({
760
- transport: this,
761
- url: envelopeToSend.url,
762
- sync: envelopeToSend.sync,
763
- headers: this.getConfiguration().requestHeaders,
764
- body: JSON.stringify(envelopeToSend.messages),
765
- onSuccess: function(responses) {
766
- var success = false;
767
- try {
768
- var received = self.convertToMessages(responses);
769
- if (received.length === 0) {
770
- self.transportFailure(envelopeToSend, request, {
771
- httpCode: 204
772
- });
773
- } else {
774
- success = true;
775
- self.transportSuccess(envelopeToSend, request, received);
776
- }
777
- } catch (x) {
778
- self._debug(x);
779
- if (!success) {
780
- self.transportFailure(envelopeToSend, request, {
781
- exception: x
782
- });
783
- }
784
- }
785
- },
786
- onError: function(reason, exception) {
787
- var failure = {
788
- reason: reason,
789
- exception: exception
790
- };
791
- if (sameStack) {
792
- // Keep the semantic of calling response callbacks asynchronously after the request
793
- self.setTimeout(function() {
794
- self.transportFailure(envelopeToSend, request, failure);
795
- }, 0);
796
- } else {
797
- self.transportFailure(envelopeToSend, request, failure);
798
- }
799
- }
800
- });
801
- sameStack = false;
802
- } catch (xx) {
803
- // Keep the semantic of calling response callbacks asynchronously after the request
804
- this.setTimeout(function() {
805
- self.transportFailure(envelopeToSend, request, {
806
- exception: xx
807
- });
808
- }, 0);
809
- }
810
- };
811
-
812
- return _self;
813
- };
814
-
815
-
816
- var WebSocketTransport = function() {
817
- var _super = new Transport();
818
- var _self = Transport.derive(_super);
819
- var _cometd;
820
- // By default WebSocket is supported
821
- var _webSocketSupported = true;
822
- // Whether we were able to establish a WebSocket connection
823
- var _webSocketConnected = false;
824
- var _stickyReconnect = true;
825
- // The context contains the envelopes that have been sent
826
- // and the timeouts for the messages that have been sent.
827
- var _context = null;
828
- var _connecting = null;
829
- var _connected = false;
830
- var _successCallback = null;
831
-
832
- _self.reset = function(init) {
833
- _super.reset(init);
834
- _webSocketSupported = true;
835
- if (init) {
836
- _webSocketConnected = false;
837
- }
838
- _stickyReconnect = true;
839
- _context = null;
840
- _connecting = null;
841
- _connected = false;
842
- };
843
-
844
- function _forceClose(context, event) {
845
- if (context) {
846
- this.webSocketClose(context, event.code, event.reason);
847
- // Force immediate failure of pending messages to trigger reconnect.
848
- // This is needed because the server may not reply to our close()
849
- // and therefore the onclose function is never called.
850
- this.onClose(context, event);
851
- }
852
- }
853
-
854
- function _sameContext(context) {
855
- return context === _connecting || context === _context;
856
- }
857
-
858
- function _storeEnvelope(context, envelope, metaConnect) {
859
- var messageIds = [];
860
- for (var i = 0; i < envelope.messages.length; ++i) {
861
- var message = envelope.messages[i];
862
- if (message.id) {
863
- messageIds.push(message.id);
864
- }
865
- }
866
- context.envelopes[messageIds.join(',')] = [envelope, metaConnect];
867
- this._debug('Transport', this.getType(), 'stored envelope, envelopes', context.envelopes);
868
- }
869
-
870
- function _websocketConnect(context) {
871
- // We may have multiple attempts to open a WebSocket
872
- // connection, for example a /meta/connect request that
873
- // may take time, along with a user-triggered publish.
874
- // Early return if we are already connecting.
875
- if (_connecting) {
876
- return;
877
- }
878
-
879
- // Mangle the URL, changing the scheme from 'http' to 'ws'.
880
- var url = _cometd.getURL().replace(/^http/, 'ws');
881
- this._debug('Transport', this.getType(), 'connecting to URL', url);
882
-
883
- try {
884
- var protocol = _cometd.getConfiguration().protocol;
885
- context.webSocket = protocol ? new window.WebSocket(url, protocol) : new window.WebSocket(url);
886
- _connecting = context;
887
- } catch (x) {
888
- _webSocketSupported = false;
889
- this._debug('Exception while creating WebSocket object', x);
890
- throw x;
891
- }
892
-
893
- // By default use sticky reconnects.
894
- _stickyReconnect = _cometd.getConfiguration().stickyReconnect !== false;
895
-
896
- var self = this;
897
- var connectTimeout = _cometd.getConfiguration().connectTimeout;
898
- if (connectTimeout > 0) {
899
- context.connectTimer = this.setTimeout(function() {
900
- _cometd._debug('Transport', self.getType(), 'timed out while connecting to URL', url, ':', connectTimeout, 'ms');
901
- // The connection was not opened, close anyway.
902
- _forceClose.call(self, context, {code: 1000, reason: 'Connect Timeout'});
903
- }, connectTimeout);
904
- }
905
-
906
- var onopen = function() {
907
- _cometd._debug('WebSocket onopen', context);
908
- if (context.connectTimer) {
909
- self.clearTimeout(context.connectTimer);
910
- }
911
-
912
- if (_sameContext(context)) {
913
- _connecting = null;
914
- _context = context;
915
- _webSocketConnected = true;
916
- self.onOpen(context);
917
- } else {
918
- // We have a valid connection already, close this one.
919
- _cometd._warn('Closing extra WebSocket connection', this, 'active connection', _context);
920
- _forceClose.call(self, context, {code: 1000, reason: 'Extra Connection'});
921
- }
922
- };
923
-
924
- // This callback is invoked when the server sends the close frame.
925
- // The close frame for a connection may arrive *after* another
926
- // connection has been opened, so we must make sure that actions
927
- // are performed only if it's the same connection.
928
- var onclose = function(event) {
929
- event = event || {code: 1000};
930
- _cometd._debug('WebSocket onclose', context, event, 'connecting', _connecting, 'current', _context);
931
-
932
- if (context.connectTimer) {
933
- self.clearTimeout(context.connectTimer);
934
- }
935
-
936
- self.onClose(context, event);
937
- };
938
-
939
- var onmessage = function(wsMessage) {
940
- _cometd._debug('WebSocket onmessage', wsMessage, context);
941
- self.onMessage(context, wsMessage);
942
- };
943
-
944
- context.webSocket.onopen = onopen;
945
- context.webSocket.onclose = onclose;
946
- context.webSocket.onerror = function() {
947
- // Clients should call onclose(), but if they do not we do it here for safety.
948
- onclose({code: 1000, reason: 'Error'});
949
- };
950
- context.webSocket.onmessage = onmessage;
951
-
952
- this._debug('Transport', this.getType(), 'configured callbacks on', context);
953
- }
954
-
955
- function _webSocketSend(context, envelope, metaConnect) {
956
- var json = JSON.stringify(envelope.messages);
957
- context.webSocket.send(json);
958
- this._debug('Transport', this.getType(), 'sent', envelope, 'metaConnect =', metaConnect);
959
-
960
- // Manage the timeout waiting for the response.
961
- var maxDelay = this.getConfiguration().maxNetworkDelay;
962
- var delay = maxDelay;
963
- if (metaConnect) {
964
- delay += this.getAdvice().timeout;
965
- _connected = true;
966
- }
967
-
968
- var self = this;
969
- var messageIds = [];
970
- for (var i = 0; i < envelope.messages.length; ++i) {
971
- (function() {
972
- var message = envelope.messages[i];
973
- if (message.id) {
974
- messageIds.push(message.id);
975
- context.timeouts[message.id] = self.setTimeout(function() {
976
- _cometd._debug('Transport', self.getType(), 'timing out message', message.id, 'after', delay, 'on', context);
977
- _forceClose.call(self, context, {code: 1000, reason: 'Message Timeout'});
978
- }, delay);
979
- }
980
- })();
981
- }
982
-
983
- this._debug('Transport', this.getType(), 'waiting at most', delay, 'ms for messages', messageIds, 'maxNetworkDelay', maxDelay, ', timeouts:', context.timeouts);
984
- }
985
-
986
- _self._notifySuccess = function(fn, messages) {
987
- fn.call(this, messages);
988
- };
989
-
990
- _self._notifyFailure = function(fn, context, messages, failure) {
991
- fn.call(this, context, messages, failure);
992
- };
993
-
994
- function _send(context, envelope, metaConnect) {
995
- try {
996
- if (context === null) {
997
- context = _connecting || {
998
- envelopes: {},
999
- timeouts: {}
1000
- };
1001
- _storeEnvelope.call(this, context, envelope, metaConnect);
1002
- _websocketConnect.call(this, context);
1003
- } else {
1004
- _storeEnvelope.call(this, context, envelope, metaConnect);
1005
- _webSocketSend.call(this, context, envelope, metaConnect);
1006
- }
1007
- } catch (x) {
1008
- // Keep the semantic of calling response callbacks asynchronously after the request.
1009
- var self = this;
1010
- this.setTimeout(function() {
1011
- _forceClose.call(self, context, {
1012
- code: 1000,
1013
- reason: 'Exception',
1014
- exception: x
1015
- });
1016
- }, 0);
1017
- }
1018
- }
1019
-
1020
- _self.onOpen = function(context) {
1021
- var envelopes = context.envelopes;
1022
- this._debug('Transport', this.getType(), 'opened', context, 'pending messages', envelopes);
1023
- for (var key in envelopes) {
1024
- if (envelopes.hasOwnProperty(key)) {
1025
- var element = envelopes[key];
1026
- var envelope = element[0];
1027
- var metaConnect = element[1];
1028
- // Store the success callback, which is independent from the envelope,
1029
- // so that it can be used to notify arrival of messages.
1030
- _successCallback = envelope.onSuccess;
1031
- _webSocketSend.call(this, context, envelope, metaConnect);
1032
- }
1033
- }
1034
- };
1035
-
1036
- _self.onMessage = function(context, wsMessage) {
1037
- this._debug('Transport', this.getType(), 'received websocket message', wsMessage, context);
1038
-
1039
- var close = false;
1040
- var messages = this.convertToMessages(wsMessage.data);
1041
- var messageIds = [];
1042
- for (var i = 0; i < messages.length; ++i) {
1043
- var message = messages[i];
1044
-
1045
- // Detect if the message is a response to a request we made.
1046
- // If it's a meta message, for sure it's a response; otherwise it's
1047
- // a publish message and publish responses don't have the data field.
1048
- if (/^\/meta\//.test(message.channel) || message.data === undefined) {
1049
- if (message.id) {
1050
- messageIds.push(message.id);
1051
-
1052
- var timeout = context.timeouts[message.id];
1053
- if (timeout) {
1054
- this.clearTimeout(timeout);
1055
- delete context.timeouts[message.id];
1056
- this._debug('Transport', this.getType(), 'removed timeout for message', message.id, ', timeouts', context.timeouts);
1057
- }
1058
- }
1059
- }
1060
-
1061
- if ('/meta/connect' === message.channel) {
1062
- _connected = false;
1063
- }
1064
- if ('/meta/disconnect' === message.channel && !_connected) {
1065
- close = true;
1066
- }
1067
- }
1068
-
1069
- // Remove the envelope corresponding to the messages.
1070
- var removed = false;
1071
- var envelopes = context.envelopes;
1072
- for (var j = 0; j < messageIds.length; ++j) {
1073
- var id = messageIds[j];
1074
- for (var key in envelopes) {
1075
- if (envelopes.hasOwnProperty(key)) {
1076
- var ids = key.split(',');
1077
- var index = Utils.inArray(id, ids);
1078
- if (index >= 0) {
1079
- removed = true;
1080
- ids.splice(index, 1);
1081
- var envelope = envelopes[key][0];
1082
- var metaConnect = envelopes[key][1];
1083
- delete envelopes[key];
1084
- if (ids.length > 0) {
1085
- envelopes[ids.join(',')] = [envelope, metaConnect];
1086
- }
1087
- break;
1088
- }
1089
- }
1090
- }
1091
- }
1092
- if (removed) {
1093
- this._debug('Transport', this.getType(), 'removed envelope, envelopes', envelopes);
1094
- }
1095
-
1096
- this._notifySuccess(_successCallback, messages);
1097
-
1098
- if (close) {
1099
- this.webSocketClose(context, 1000, 'Disconnect');
1100
- }
1101
- };
1102
-
1103
- _self.onClose = function(context, event) {
1104
- this._debug('Transport', this.getType(), 'closed', context, event);
1105
-
1106
- if (_sameContext(context)) {
1107
- // Remember if we were able to connect.
1108
- // This close event could be due to server shutdown,
1109
- // and if it restarts we want to try websocket again.
1110
- _webSocketSupported = _stickyReconnect && _webSocketConnected;
1111
- _connecting = null;
1112
- _context = null;
1113
- }
1114
-
1115
- var timeouts = context.timeouts;
1116
- context.timeouts = {};
1117
- for (var id in timeouts) {
1118
- if (timeouts.hasOwnProperty(id)) {
1119
- this.clearTimeout(timeouts[id]);
1120
- }
1121
- }
1122
-
1123
- var envelopes = context.envelopes;
1124
- context.envelopes = {};
1125
- for (var key in envelopes) {
1126
- if (envelopes.hasOwnProperty(key)) {
1127
- var envelope = envelopes[key][0];
1128
- var metaConnect = envelopes[key][1];
1129
- if (metaConnect) {
1130
- _connected = false;
1131
- }
1132
- var failure = {
1133
- websocketCode: event.code,
1134
- reason: event.reason
1135
- };
1136
- if (event.exception) {
1137
- failure.exception = event.exception;
1138
- }
1139
- this._notifyFailure(envelope.onFailure, context, envelope.messages, failure);
1140
- }
1141
- }
1142
- };
1143
-
1144
- _self.registered = function(type, cometd) {
1145
- _super.registered(type, cometd);
1146
- _cometd = cometd;
1147
- };
1148
-
1149
- _self.accept = function(version, crossDomain, url) {
1150
- this._debug('Transport', this.getType(), 'accept, supported:', _webSocketSupported);
1151
- // Using !! to return a boolean (and not the WebSocket object).
1152
- return _webSocketSupported && !!window.WebSocket && _cometd.websocketEnabled !== false;
1153
- };
1154
-
1155
- _self.send = function(envelope, metaConnect) {
1156
- this._debug('Transport', this.getType(), 'sending', envelope, 'metaConnect =', metaConnect);
1157
- _send.call(this, _context, envelope, metaConnect);
1158
- };
1159
-
1160
- _self.webSocketClose = function(context, code, reason) {
1161
- try {
1162
- if (context.webSocket) {
1163
- context.webSocket.close(code, reason);
1164
- }
1165
- } catch (x) {
1166
- this._debug(x);
1167
- }
1168
- };
1169
-
1170
- _self.abort = function() {
1171
- _super.abort();
1172
- _forceClose.call(this, _context, {code: 1000, reason: 'Abort'});
1173
- this.reset(true);
1174
- };
1175
-
1176
- return _self;
1177
- };
1178
-
1179
-
1180
- /**
1181
- * The constructor for a CometD object, identified by an optional name.
1182
- * The default name is the string 'default'.
1183
- * @param name the optional name of this cometd object
1184
- */
1185
- var CometD = function (name) {
1186
- var _cometd = this;
1187
- var _name = name || 'default';
1188
- var _crossDomain = false;
1189
- var _transports = new TransportRegistry();
1190
- var _transport;
1191
- var _status = 'disconnected';
1192
- var _messageId = 0;
1193
- var _clientId = null;
1194
- var _batch = 0;
1195
- var _messageQueue = [];
1196
- var _internalBatch = false;
1197
- var _listenerId = 0;
1198
- var _listeners = {};
1199
- var _backoff = 0;
1200
- var _scheduledSend = null;
1201
- var _extensions = [];
1202
- var _advice = {};
1203
- var _handshakeProps;
1204
- var _handshakeCallback;
1205
- var _callbacks = {};
1206
- var _remoteCalls = {};
1207
- var _reestablish = false;
1208
- var _connected = false;
1209
- var _unconnectTime = 0;
1210
- var _handshakeMessages = 0;
1211
- var _config = {
1212
- protocol: null,
1213
- stickyReconnect: true,
1214
- connectTimeout: 0,
1215
- maxConnections: 2,
1216
- backoffIncrement: 1000,
1217
- maxBackoff: 60000,
1218
- logLevel: 'info',
1219
- reverseIncomingExtensions: true,
1220
- maxNetworkDelay: 10000,
1221
- requestHeaders: {},
1222
- appendMessageTypeToURL: true,
1223
- autoBatch: false,
1224
- urls: {},
1225
- maxURILength: 2000,
1226
- advice: {
1227
- timeout: 60000,
1228
- interval: 0,
1229
- reconnect: undefined,
1230
- maxInterval: 0
1231
- }
1232
- };
1233
-
1234
- function _fieldValue(object, name) {
1235
- try {
1236
- return object[name];
1237
- } catch (x) {
1238
- return undefined;
1239
- }
1240
- }
1241
-
1242
- /**
1243
- * Mixes in the given objects into the target object by copying the properties.
1244
- * @param deep if the copy must be deep
1245
- * @param target the target object
1246
- * @param objects the objects whose properties are copied into the target
1247
- */
1248
- this._mixin = function(deep, target, objects) {
1249
- var result = target || {};
1250
-
1251
- // Skip first 2 parameters (deep and target), and loop over the others
1252
- for (var i = 2; i < arguments.length; ++i) {
1253
- var object = arguments[i];
1254
-
1255
- if (object === undefined || object === null) {
1256
- continue;
1257
- }
1258
-
1259
- for (var propName in object) {
1260
- if (object.hasOwnProperty(propName)) {
1261
- var prop = _fieldValue(object, propName);
1262
- var targ = _fieldValue(result, propName);
1263
-
1264
- // Avoid infinite loops
1265
- if (prop === target) {
1266
- continue;
1267
- }
1268
- // Do not mixin undefined values
1269
- if (prop === undefined) {
1270
- continue;
1271
- }
1272
-
1273
- if (deep && typeof prop === 'object' && prop !== null) {
1274
- if (prop instanceof Array) {
1275
- result[propName] = this._mixin(deep, targ instanceof Array ? targ : [], prop);
1276
- } else {
1277
- var source = typeof targ === 'object' && !(targ instanceof Array) ? targ : {};
1278
- result[propName] = this._mixin(deep, source, prop);
1279
- }
1280
- } else {
1281
- result[propName] = prop;
1282
- }
1283
- }
1284
- }
1285
- }
1286
-
1287
- return result;
1288
- };
1289
-
1290
- function _isString(value) {
1291
- return Utils.isString(value);
1292
- }
1293
-
1294
- function _isFunction(value) {
1295
- if (value === undefined || value === null) {
1296
- return false;
1297
- }
1298
- return typeof value === 'function';
1299
- }
1300
-
1301
- function _zeroPad(value, length) {
1302
- var result = '';
1303
- while (--length > 0) {
1304
- if (value >= Math.pow(10, length)) {
1305
- break;
1306
- }
1307
- result += '0';
1308
- }
1309
- result += value;
1310
- return result;
1311
- }
1312
-
1313
- function _log(level, args) {
1314
- if (window.console) {
1315
- var logger = window.console[level];
1316
- if (_isFunction(logger)) {
1317
- var now = new Date();
1318
- [].splice.call(args, 0, 0, _zeroPad(now.getHours(), 2) + ':' + _zeroPad(now.getMinutes(), 2) + ':' +
1319
- _zeroPad(now.getSeconds(), 2) + '.' + _zeroPad(now.getMilliseconds(), 3));
1320
- logger.apply(window.console, args);
1321
- }
1322
- }
1323
- }
1324
-
1325
- this._warn = function() {
1326
- _log('warn', arguments);
1327
- };
1328
-
1329
- this._info = function() {
1330
- if (_config.logLevel !== 'warn') {
1331
- _log('info', arguments);
1332
- }
1333
- };
1334
-
1335
- this._debug = function() {
1336
- if (_config.logLevel === 'debug') {
1337
- _log('debug', arguments);
1338
- }
1339
- };
1340
-
1341
- function _splitURL(url) {
1342
- // [1] = protocol://,
1343
- // [2] = host:port,
1344
- // [3] = host,
1345
- // [4] = IPv6_host,
1346
- // [5] = IPv4_host,
1347
- // [6] = :port,
1348
- // [7] = port,
1349
- // [8] = uri,
1350
- // [9] = rest (query / fragment)
1351
- return /(^https?:\/\/)?(((\[[^\]]+\])|([^:\/\?#]+))(:(\d+))?)?([^\?#]*)(.*)?/.exec(url);
1352
- }
1353
-
1354
- /**
1355
- * Returns whether the given hostAndPort is cross domain.
1356
- * The default implementation checks against window.location.host
1357
- * but this function can be overridden to make it work in non-browser
1358
- * environments.
1359
- *
1360
- * @param hostAndPort the host and port in format host:port
1361
- * @return whether the given hostAndPort is cross domain
1362
- */
1363
- this._isCrossDomain = function(hostAndPort) {
1364
- return hostAndPort && hostAndPort !== window.location.host;
1365
- };
1366
-
1367
- function _configure(configuration) {
1368
- _cometd._debug('Configuring cometd object with', configuration);
1369
- // Support old style param, where only the Bayeux server URL was passed
1370
- if (_isString(configuration)) {
1371
- configuration = { url: configuration };
1372
- }
1373
- if (!configuration) {
1374
- configuration = {};
1375
- }
1376
-
1377
- _config = _cometd._mixin(false, _config, configuration);
1378
-
1379
- var url = _cometd.getURL();
1380
- if (!url) {
1381
- throw 'Missing required configuration parameter \'url\' specifying the Bayeux server URL';
1382
- }
1383
-
1384
- // Check if we're cross domain.
1385
- var urlParts = _splitURL(url);
1386
- var hostAndPort = urlParts[2];
1387
- var uri = urlParts[8];
1388
- var afterURI = urlParts[9];
1389
- _crossDomain = _cometd._isCrossDomain(hostAndPort);
1390
-
1391
- // Check if appending extra path is supported
1392
- if (_config.appendMessageTypeToURL) {
1393
- if (afterURI !== undefined && afterURI.length > 0) {
1394
- _cometd._info('Appending message type to URI ' + uri + afterURI + ' is not supported, disabling \'appendMessageTypeToURL\' configuration');
1395
- _config.appendMessageTypeToURL = false;
1396
- } else {
1397
- var uriSegments = uri.split('/');
1398
- var lastSegmentIndex = uriSegments.length - 1;
1399
- if (uri.match(/\/$/)) {
1400
- lastSegmentIndex -= 1;
1401
- }
1402
- if (uriSegments[lastSegmentIndex].indexOf('.') >= 0) {
1403
- // Very likely the CometD servlet's URL pattern is mapped to an extension, such as *.cometd
1404
- // It will be difficult to add the extra path in this case
1405
- _cometd._info('Appending message type to URI ' + uri + ' is not supported, disabling \'appendMessageTypeToURL\' configuration');
1406
- _config.appendMessageTypeToURL = false;
1407
- }
1408
- }
1409
- }
1410
- }
1411
-
1412
- function _removeListener(subscription) {
1413
- if (subscription) {
1414
- var subscriptions = _listeners[subscription.channel];
1415
- if (subscriptions && subscriptions[subscription.id]) {
1416
- delete subscriptions[subscription.id];
1417
- _cometd._debug('Removed', subscription.listener ? 'listener' : 'subscription', subscription);
1418
- }
1419
- }
1420
- }
1421
-
1422
- function _removeSubscription(subscription) {
1423
- if (subscription && !subscription.listener) {
1424
- _removeListener(subscription);
1425
- }
1426
- }
1427
-
1428
- function _clearSubscriptions() {
1429
- for (var channel in _listeners) {
1430
- if (_listeners.hasOwnProperty(channel)) {
1431
- var subscriptions = _listeners[channel];
1432
- if (subscriptions) {
1433
- for (var id in subscriptions) {
1434
- if (subscriptions.hasOwnProperty(id)) {
1435
- _removeSubscription(subscriptions[id]);
1436
- }
1437
- }
1438
- }
1439
- }
1440
- }
1441
- }
1442
-
1443
- function _setStatus(newStatus) {
1444
- if (_status !== newStatus) {
1445
- _cometd._debug('Status', _status, '->', newStatus);
1446
- _status = newStatus;
1447
- }
1448
- }
1449
-
1450
- function _isDisconnected() {
1451
- return _status === 'disconnecting' || _status === 'disconnected';
1452
- }
1453
-
1454
- function _nextMessageId() {
1455
- var result = ++_messageId;
1456
- return '' + result;
1457
- }
1458
-
1459
- function _applyExtension(scope, callback, name, message, outgoing) {
1460
- try {
1461
- return callback.call(scope, message);
1462
- } catch (x) {
1463
- var handler = _cometd.onExtensionException;
1464
- if (_isFunction(handler)) {
1465
- _cometd._debug('Invoking extension exception handler', name, x);
1466
- try {
1467
- handler.call(_cometd, x, name, outgoing, message);
1468
- } catch (xx) {
1469
- _cometd._info('Exception during execution of extension exception handler', name, xx);
1470
- }
1471
- } else {
1472
- _cometd._info('Exception during execution of extension', name, x);
1473
- }
1474
- return message;
1475
- }
1476
- }
1477
-
1478
- function _applyIncomingExtensions(message) {
1479
- for (var i = 0; i < _extensions.length; ++i) {
1480
- if (message === undefined || message === null) {
1481
- break;
1482
- }
1483
-
1484
- var index = _config.reverseIncomingExtensions ? _extensions.length - 1 - i : i;
1485
- var extension = _extensions[index];
1486
- var callback = extension.extension.incoming;
1487
- if (_isFunction(callback)) {
1488
- var result = _applyExtension(extension.extension, callback, extension.name, message, false);
1489
- message = result === undefined ? message : result;
1490
- }
1491
- }
1492
- return message;
1493
- }
1494
-
1495
- function _applyOutgoingExtensions(message) {
1496
- for (var i = 0; i < _extensions.length; ++i) {
1497
- if (message === undefined || message === null) {
1498
- break;
1499
- }
1500
-
1501
- var extension = _extensions[i];
1502
- var callback = extension.extension.outgoing;
1503
- if (_isFunction(callback)) {
1504
- var result = _applyExtension(extension.extension, callback, extension.name, message, true);
1505
- message = result === undefined ? message : result;
1506
- }
1507
- }
1508
- return message;
1509
- }
1510
-
1511
- function _notify(channel, message) {
1512
- var subscriptions = _listeners[channel];
1513
- if (subscriptions) {
1514
- for (var id in subscriptions) {
1515
- if (subscriptions.hasOwnProperty(id)) {
1516
- var subscription = subscriptions[id];
1517
- // Subscriptions may come and go, so the array may have 'holes'
1518
- if (subscription) {
1519
- try {
1520
- subscription.callback.call(subscription.scope, message);
1521
- } catch (x) {
1522
- var handler = _cometd.onListenerException;
1523
- if (_isFunction(handler)) {
1524
- _cometd._debug('Invoking listener exception handler', subscription, x);
1525
- try {
1526
- handler.call(_cometd, x, subscription, subscription.listener, message);
1527
- } catch (xx) {
1528
- _cometd._info('Exception during execution of listener exception handler', subscription, xx);
1529
- }
1530
- } else {
1531
- _cometd._info('Exception during execution of listener', subscription, message, x);
1532
- }
1533
- }
1534
- }
1535
- }
1536
- }
1537
- }
1538
- }
1539
-
1540
- function _notifyListeners(channel, message) {
1541
- // Notify direct listeners
1542
- _notify(channel, message);
1543
-
1544
- // Notify the globbing listeners
1545
- var channelParts = channel.split('/');
1546
- var last = channelParts.length - 1;
1547
- for (var i = last; i > 0; --i) {
1548
- var channelPart = channelParts.slice(0, i).join('/') + '/*';
1549
- // We don't want to notify /foo/* if the channel is /foo/bar/baz,
1550
- // so we stop at the first non recursive globbing
1551
- if (i === last) {
1552
- _notify(channelPart, message);
1553
- }
1554
- // Add the recursive globber and notify
1555
- channelPart += '*';
1556
- _notify(channelPart, message);
1557
- }
1558
- }
1559
-
1560
- function _cancelDelayedSend() {
1561
- if (_scheduledSend !== null) {
1562
- Utils.clearTimeout(_scheduledSend);
1563
- }
1564
- _scheduledSend = null;
1565
- }
1566
-
1567
- function _delayedSend(operation, delay) {
1568
- _cancelDelayedSend();
1569
- var time = _advice.interval + delay;
1570
- _cometd._debug('Function scheduled in', time, 'ms, interval =', _advice.interval, 'backoff =', _backoff, operation);
1571
- _scheduledSend = Utils.setTimeout(_cometd, operation, time);
1572
- }
1573
-
1574
- // Needed to break cyclic dependencies between function definitions
1575
- var _handleMessages;
1576
- var _handleFailure;
1577
-
1578
- /**
1579
- * Delivers the messages to the CometD server
1580
- * @param sync whether the send is synchronous
1581
- * @param messages the array of messages to send
1582
- * @param metaConnect true if this send is on /meta/connect
1583
- * @param extraPath an extra path to append to the Bayeux server URL
1584
- */
1585
- function _send(sync, messages, metaConnect, extraPath) {
1586
- // We must be sure that the messages have a clientId.
1587
- // This is not guaranteed since the handshake may take time to return
1588
- // (and hence the clientId is not known yet) and the application
1589
- // may create other messages.
1590
- for (var i = 0; i < messages.length; ++i) {
1591
- var message = messages[i];
1592
- var messageId = message.id;
1593
-
1594
- if (_clientId) {
1595
- message.clientId = _clientId;
1596
- }
1597
-
1598
- message = _applyOutgoingExtensions(message);
1599
- if (message !== undefined && message !== null) {
1600
- // Extensions may have modified the message id, but we need to own it.
1601
- message.id = messageId;
1602
- messages[i] = message;
1603
- } else {
1604
- delete _callbacks[messageId];
1605
- messages.splice(i--, 1);
1606
- }
1607
- }
1608
-
1609
- if (messages.length === 0) {
1610
- return;
1611
- }
1612
-
1613
- var url = _cometd.getURL();
1614
- if (_config.appendMessageTypeToURL) {
1615
- // If url does not end with '/', then append it
1616
- if (!url.match(/\/$/)) {
1617
- url = url + '/';
1618
- }
1619
- if (extraPath) {
1620
- url = url + extraPath;
1621
- }
1622
- }
1623
-
1624
- var envelope = {
1625
- url: url,
1626
- sync: sync,
1627
- messages: messages,
1628
- onSuccess: function(rcvdMessages) {
1629
- try {
1630
- _handleMessages.call(_cometd, rcvdMessages);
1631
- } catch (x) {
1632
- _cometd._info('Exception during handling of messages', x);
1633
- }
1634
- },
1635
- onFailure: function(conduit, messages, failure) {
1636
- try {
1637
- var transport = _cometd.getTransport();
1638
- failure.connectionType = transport ? transport.getType() : "unknown";
1639
- _handleFailure.call(_cometd, conduit, messages, failure);
1640
- } catch (x) {
1641
- _cometd._info('Exception during handling of failure', x);
1642
- }
1643
- }
1644
- };
1645
- _cometd._debug('Send', envelope);
1646
- _transport.send(envelope, metaConnect);
1647
- }
1648
-
1649
- function _queueSend(message) {
1650
- if (_batch > 0 || _internalBatch === true) {
1651
- _messageQueue.push(message);
1652
- } else {
1653
- _send(false, [message], false);
1654
- }
1655
- }
1656
-
1657
- /**
1658
- * Sends a complete bayeux message.
1659
- * This method is exposed as a public so that extensions may use it
1660
- * to send bayeux message directly, for example in case of re-sending
1661
- * messages that have already been sent but that for some reason must
1662
- * be resent.
1663
- */
1664
- this.send = _queueSend;
1665
-
1666
- function _resetBackoff() {
1667
- _backoff = 0;
1668
- }
1669
-
1670
- function _increaseBackoff() {
1671
- if (_backoff < _config.maxBackoff) {
1672
- _backoff += _config.backoffIncrement;
1673
- }
1674
- return _backoff;
1675
- }
1676
-
1677
- /**
1678
- * Starts a the batch of messages to be sent in a single request.
1679
- * @see #_endBatch(sendMessages)
1680
- */
1681
- function _startBatch() {
1682
- ++_batch;
1683
- _cometd._debug('Starting batch, depth', _batch);
1684
- }
1685
-
1686
- function _flushBatch() {
1687
- var messages = _messageQueue;
1688
- _messageQueue = [];
1689
- if (messages.length > 0) {
1690
- _send(false, messages, false);
1691
- }
1692
- }
1693
-
1694
- /**
1695
- * Ends the batch of messages to be sent in a single request,
1696
- * optionally sending messages present in the message queue depending
1697
- * on the given argument.
1698
- * @see #_startBatch()
1699
- */
1700
- function _endBatch() {
1701
- --_batch;
1702
- _cometd._debug('Ending batch, depth', _batch);
1703
- if (_batch < 0) {
1704
- throw 'Calls to startBatch() and endBatch() are not paired';
1705
- }
1706
-
1707
- if (_batch === 0 && !_isDisconnected() && !_internalBatch) {
1708
- _flushBatch();
1709
- }
1710
- }
1711
-
1712
- /**
1713
- * Sends the connect message
1714
- */
1715
- function _connect() {
1716
- if (!_isDisconnected()) {
1717
- var bayeuxMessage = {
1718
- id: _nextMessageId(),
1719
- channel: '/meta/connect',
1720
- connectionType: _transport.getType()
1721
- };
1722
-
1723
- // In case of reload or temporary loss of connection
1724
- // we want the next successful connect to return immediately
1725
- // instead of being held by the server, so that connect listeners
1726
- // can be notified that the connection has been re-established
1727
- if (!_connected) {
1728
- bayeuxMessage.advice = { timeout: 0 };
1729
- }
1730
-
1731
- _setStatus('connecting');
1732
- _cometd._debug('Connect sent', bayeuxMessage);
1733
- _send(false, [bayeuxMessage], true, 'connect');
1734
- _setStatus('connected');
1735
- }
1736
- }
1737
-
1738
- function _delayedConnect(delay) {
1739
- _setStatus('connecting');
1740
- _delayedSend(function() {
1741
- _connect();
1742
- }, delay);
1743
- }
1744
-
1745
- function _updateAdvice(newAdvice) {
1746
- if (newAdvice) {
1747
- _advice = _cometd._mixin(false, {}, _config.advice, newAdvice);
1748
- _cometd._debug('New advice', _advice);
1749
- }
1750
- }
1751
-
1752
- function _disconnect(abort) {
1753
- _cancelDelayedSend();
1754
- if (abort && _transport) {
1755
- _transport.abort();
1756
- }
1757
- _clientId = null;
1758
- _setStatus('disconnected');
1759
- _batch = 0;
1760
- _resetBackoff();
1761
- _transport = null;
1762
- _reestablish = false;
1763
- _connected = false;
1764
-
1765
- // Fail any existing queued message
1766
- if (_messageQueue.length > 0) {
1767
- var messages = _messageQueue;
1768
- _messageQueue = [];
1769
- _handleFailure.call(_cometd, undefined, messages, {
1770
- reason: 'Disconnected'
1771
- });
1772
- }
1773
- }
1774
-
1775
- function _notifyTransportException(oldTransport, newTransport, failure) {
1776
- var handler = _cometd.onTransportException;
1777
- if (_isFunction(handler)) {
1778
- _cometd._debug('Invoking transport exception handler', oldTransport, newTransport, failure);
1779
- try {
1780
- handler.call(_cometd, failure, oldTransport, newTransport);
1781
- } catch (x) {
1782
- _cometd._info('Exception during execution of transport exception handler', x);
1783
- }
1784
- }
1785
- }
1786
-
1787
- /**
1788
- * Sends the initial handshake message
1789
- */
1790
- function _handshake(handshakeProps, handshakeCallback) {
1791
- if (_isFunction(handshakeProps)) {
1792
- handshakeCallback = handshakeProps;
1793
- handshakeProps = undefined;
1794
- }
1795
-
1796
- _clientId = null;
1797
-
1798
- _clearSubscriptions();
1799
-
1800
- // Reset the transports if we're not retrying the handshake
1801
- if (_isDisconnected()) {
1802
- _transports.reset(true);
1803
- _updateAdvice(_config.advice);
1804
- }
1805
-
1806
- _batch = 0;
1807
-
1808
- // Mark the start of an internal batch.
1809
- // This is needed because handshake and connect are async.
1810
- // It may happen that the application calls init() then subscribe()
1811
- // and the subscribe message is sent before the connect message, if
1812
- // the subscribe message is not held until the connect message is sent.
1813
- // So here we start a batch to hold temporarily any message until
1814
- // the connection is fully established.
1815
- _internalBatch = true;
1816
-
1817
- // Save the properties provided by the user, so that
1818
- // we can reuse them during automatic re-handshake
1819
- _handshakeProps = handshakeProps;
1820
- _handshakeCallback = handshakeCallback;
1821
-
1822
- var version = '1.0';
1823
-
1824
- // Figure out the transports to send to the server
1825
- var url = _cometd.getURL();
1826
- var transportTypes = _transports.findTransportTypes(version, _crossDomain, url);
1827
-
1828
- var bayeuxMessage = {
1829
- id: _nextMessageId(),
1830
- version: version,
1831
- minimumVersion: version,
1832
- channel: '/meta/handshake',
1833
- supportedConnectionTypes: transportTypes,
1834
- advice: {
1835
- timeout: _advice.timeout,
1836
- interval: _advice.interval
1837
- }
1838
- };
1839
- // Do not allow the user to override important fields.
1840
- var message = _cometd._mixin(false, {}, _handshakeProps, bayeuxMessage);
1841
-
1842
- // Save the callback.
1843
- _cometd._putCallback(message.id, handshakeCallback);
1844
-
1845
- // Pick up the first available transport as initial transport
1846
- // since we don't know if the server supports it
1847
- if (!_transport) {
1848
- _transport = _transports.negotiateTransport(transportTypes, version, _crossDomain, url);
1849
- if (!_transport) {
1850
- var failure = 'Could not find initial transport among: ' + _transports.getTransportTypes();
1851
- _cometd._warn(failure);
1852
- throw failure;
1853
- }
1854
- }
1855
-
1856
- _cometd._debug('Initial transport is', _transport.getType());
1857
-
1858
- // We started a batch to hold the application messages,
1859
- // so here we must bypass it and send immediately.
1860
- _setStatus('handshaking');
1861
- _cometd._debug('Handshake sent', message);
1862
- _send(false, [message], false, 'handshake');
1863
- }
1864
-
1865
- function _delayedHandshake(delay) {
1866
- _setStatus('handshaking');
1867
-
1868
- // We will call _handshake() which will reset _clientId, but we want to avoid
1869
- // that between the end of this method and the call to _handshake() someone may
1870
- // call publish() (or other methods that call _queueSend()).
1871
- _internalBatch = true;
1872
-
1873
- _delayedSend(function() {
1874
- _handshake(_handshakeProps, _handshakeCallback);
1875
- }, delay);
1876
- }
1877
-
1878
- function _notifyCallback(callback, message) {
1879
- try {
1880
- callback.call(_cometd, message);
1881
- } catch (x) {
1882
- var handler = _cometd.onCallbackException;
1883
- if (_isFunction(handler)) {
1884
- _cometd._debug('Invoking callback exception handler', x);
1885
- try {
1886
- handler.call(_cometd, x, message);
1887
- } catch (xx) {
1888
- _cometd._info('Exception during execution of callback exception handler', xx);
1889
- }
1890
- } else {
1891
- _cometd._info('Exception during execution of message callback', x);
1892
- }
1893
- }
1894
- }
1895
-
1896
- this._getCallback = function(messageId) {
1897
- return _callbacks[messageId];
1898
- };
1899
-
1900
- this._putCallback = function(messageId, callback) {
1901
- var result = this._getCallback(messageId);
1902
- if (_isFunction(callback)) {
1903
- _callbacks[messageId] = callback;
1904
- }
1905
- return result;
1906
- };
1907
-
1908
- function _handleCallback(message) {
1909
- var callback = _cometd._getCallback([message.id]);
1910
- if (_isFunction(callback)) {
1911
- delete _callbacks[message.id];
1912
- _notifyCallback(callback, message);
1913
- }
1914
- }
1915
-
1916
- function _handleRemoteCall(message) {
1917
- var context = _remoteCalls[message.id];
1918
- delete _remoteCalls[message.id];
1919
- if (context) {
1920
- _cometd._debug('Handling remote call response for', message, 'with context', context);
1921
-
1922
- // Clear the timeout, if present.
1923
- var timeout = context.timeout;
1924
- if (timeout) {
1925
- Utils.clearTimeout(timeout);
1926
- }
1927
-
1928
- var callback = context.callback;
1929
- if (_isFunction(callback)) {
1930
- _notifyCallback(callback, message);
1931
- return true;
1932
- }
1933
- }
1934
- return false;
1935
- }
1936
-
1937
- this.onTransportFailure = function(message, failureInfo, failureHandler) {
1938
- this._debug('Transport failure', failureInfo, 'for', message);
1939
-
1940
- var transports = this.getTransportRegistry();
1941
- var url = this.getURL();
1942
- var crossDomain = this._isCrossDomain(_splitURL(url)[2]);
1943
- var version = '1.0';
1944
- var transportTypes = transports.findTransportTypes(version, crossDomain, url);
1945
-
1946
- if (failureInfo.action === 'none') {
1947
- if (message.channel === '/meta/handshake') {
1948
- if (!failureInfo.transport) {
1949
- var failure = 'Could not negotiate transport, client=[' + transportTypes + '], server=[' + message.supportedConnectionTypes + ']';
1950
- this._warn(failure);
1951
- _notifyTransportException(_transport.getType(), null, {
1952
- reason: failure,
1953
- connectionType: _transport.getType(),
1954
- transport: _transport
1955
- });
1956
- }
1957
- }
1958
- } else {
1959
- failureInfo.delay = this.getBackoffPeriod();
1960
- // Different logic depending on whether we are handshaking or connecting.
1961
- if (message.channel === '/meta/handshake') {
1962
- if (!failureInfo.transport) {
1963
- // The transport is invalid, try to negotiate again.
1964
- var newTransport = transports.negotiateTransport(transportTypes, version, crossDomain, url);
1965
- if (!newTransport) {
1966
- this._warn('Could not negotiate transport, client=[' + transportTypes + ']');
1967
- _notifyTransportException(_transport.getType(), null, message.failure);
1968
- failureInfo.action = 'none';
1969
- } else {
1970
- this._debug('Transport', _transport.getType(), '->', newTransport.getType());
1971
- _notifyTransportException(_transport.getType(), newTransport.getType(), message.failure);
1972
- failureInfo.action = 'handshake';
1973
- failureInfo.transport = newTransport;
1974
- }
1975
- }
1976
-
1977
- if (failureInfo.action !== 'none') {
1978
- this.increaseBackoffPeriod();
1979
- }
1980
- } else {
1981
- var now = new Date().getTime();
1982
-
1983
- if (_unconnectTime === 0) {
1984
- _unconnectTime = now;
1985
- }
1986
-
1987
- if (failureInfo.action === 'retry') {
1988
- failureInfo.delay = this.increaseBackoffPeriod();
1989
- // Check whether we may switch to handshaking.
1990
- var maxInterval = _advice.maxInterval;
1991
- if (maxInterval > 0) {
1992
- var expiration = _advice.timeout + _advice.interval + maxInterval;
1993
- var unconnected = now - _unconnectTime;
1994
- if (unconnected + _backoff > expiration) {
1995
- failureInfo.action = 'handshake';
1996
- }
1997
- }
1998
- }
1999
-
2000
- if (failureInfo.action === 'handshake') {
2001
- failureInfo.delay = 0;
2002
- transports.reset(false);
2003
- this.resetBackoffPeriod();
2004
- }
2005
- }
2006
- }
2007
-
2008
- failureHandler.call(_cometd, failureInfo);
2009
- };
2010
-
2011
- function _handleTransportFailure(failureInfo) {
2012
- _cometd._debug('Transport failure handling', failureInfo);
2013
-
2014
- if (failureInfo.transport) {
2015
- _transport = failureInfo.transport;
2016
- }
2017
-
2018
- if (failureInfo.url) {
2019
- _transport.setURL(failureInfo.url);
2020
- }
2021
-
2022
- var action = failureInfo.action;
2023
- var delay = failureInfo.delay || 0;
2024
- switch (action) {
2025
- case 'handshake':
2026
- _delayedHandshake(delay);
2027
- break;
2028
- case 'retry':
2029
- _delayedConnect(delay);
2030
- break;
2031
- case 'none':
2032
- _disconnect(true);
2033
- break;
2034
- default:
2035
- throw 'Unknown action ' + action;
2036
- }
2037
- }
2038
-
2039
- function _failHandshake(message, failureInfo) {
2040
- _handleCallback(message);
2041
- _notifyListeners('/meta/handshake', message);
2042
- _notifyListeners('/meta/unsuccessful', message);
2043
-
2044
- // The listeners may have disconnected.
2045
- if (_isDisconnected()) {
2046
- failureInfo.action = 'none';
2047
- }
2048
-
2049
- _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
2050
- }
2051
-
2052
- function _handshakeResponse(message) {
2053
- var url = _cometd.getURL();
2054
- if (message.successful) {
2055
- var crossDomain = _cometd._isCrossDomain(_splitURL(url)[2]);
2056
- var newTransport = _transports.negotiateTransport(message.supportedConnectionTypes, message.version, crossDomain, url);
2057
- if (newTransport === null) {
2058
- message.successful = false;
2059
- _failHandshake(message, {
2060
- cause: 'negotiation',
2061
- action: 'none',
2062
- transport: null
2063
- });
2064
- return;
2065
- } else if (_transport !== newTransport) {
2066
- _cometd._debug('Transport', _transport.getType(), '->', newTransport.getType());
2067
- _transport = newTransport;
2068
- }
2069
-
2070
- _clientId = message.clientId;
2071
-
2072
- // End the internal batch and allow held messages from the application
2073
- // to go to the server (see _handshake() where we start the internal batch).
2074
- _internalBatch = false;
2075
- _flushBatch();
2076
-
2077
- // Here the new transport is in place, as well as the clientId, so
2078
- // the listeners can perform a publish() if they want.
2079
- // Notify the listeners before the connect below.
2080
- message.reestablish = _reestablish;
2081
- _reestablish = true;
2082
-
2083
- _handleCallback(message);
2084
- _notifyListeners('/meta/handshake', message);
2085
-
2086
- _handshakeMessages = message['x-messages'] || 0;
2087
-
2088
- var action = _isDisconnected() ? 'none' : _advice.reconnect || 'retry';
2089
- switch (action) {
2090
- case 'retry':
2091
- _resetBackoff();
2092
- if (_handshakeMessages === 0) {
2093
- _delayedConnect(0);
2094
- } else {
2095
- _cometd._debug('Processing', _handshakeMessages, 'handshake-delivered messages');
2096
- }
2097
- break;
2098
- case 'none':
2099
- _disconnect(true);
2100
- break;
2101
- default:
2102
- throw 'Unrecognized advice action ' + action;
2103
- }
2104
- } else {
2105
- _failHandshake(message, {
2106
- cause: 'unsuccessful',
2107
- action: _advice.reconnect || 'handshake',
2108
- transport: _transport
2109
- });
2110
- }
2111
- }
2112
-
2113
- function _handshakeFailure(message) {
2114
- _failHandshake(message, {
2115
- cause: 'failure',
2116
- action: 'handshake',
2117
- transport: null
2118
- });
2119
- }
2120
-
2121
- function _failConnect(message, failureInfo) {
2122
- // Notify the listeners after the status change but before the next action.
2123
- _notifyListeners('/meta/connect', message);
2124
- _notifyListeners('/meta/unsuccessful', message);
2125
-
2126
- // The listeners may have disconnected.
2127
- if (_isDisconnected()) {
2128
- failureInfo.action = 'none';
2129
- }
2130
-
2131
- _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
2132
- }
2133
-
2134
- function _connectResponse(message) {
2135
- _connected = message.successful;
2136
-
2137
- if (_connected) {
2138
- _notifyListeners('/meta/connect', message);
2139
-
2140
- // Normally, the advice will say "reconnect: 'retry', interval: 0"
2141
- // and the server will hold the request, so when a response returns
2142
- // we immediately call the server again (long polling).
2143
- // Listeners can call disconnect(), so check the state after they run.
2144
- var action = _isDisconnected() ? 'none' : _advice.reconnect || 'retry';
2145
- switch (action) {
2146
- case 'retry':
2147
- _resetBackoff();
2148
- _delayedConnect(_backoff);
2149
- break;
2150
- case 'none':
2151
- _disconnect(false);
2152
- break;
2153
- default:
2154
- throw 'Unrecognized advice action ' + action;
2155
- }
2156
- } else {
2157
- _failConnect(message, {
2158
- cause: 'unsuccessful',
2159
- action: _advice.reconnect || 'retry',
2160
- transport: _transport
2161
- });
2162
- }
2163
- }
2164
-
2165
- function _connectFailure(message) {
2166
- _connected = false;
2167
-
2168
- _failConnect(message, {
2169
- cause: 'failure',
2170
- action: 'retry',
2171
- transport: null
2172
- });
2173
- }
2174
-
2175
- function _failDisconnect(message) {
2176
- _disconnect(true);
2177
- _handleCallback(message);
2178
- _notifyListeners('/meta/disconnect', message);
2179
- _notifyListeners('/meta/unsuccessful', message);
2180
- }
2181
-
2182
- function _disconnectResponse(message) {
2183
- if (message.successful) {
2184
- // Wait for the /meta/connect to arrive.
2185
- _disconnect(false);
2186
- _handleCallback(message);
2187
- _notifyListeners('/meta/disconnect', message);
2188
- } else {
2189
- _failDisconnect(message);
2190
- }
2191
- }
2192
-
2193
- function _disconnectFailure(message) {
2194
- _failDisconnect(message);
2195
- }
2196
-
2197
- function _failSubscribe(message) {
2198
- var subscriptions = _listeners[message.subscription];
2199
- if (subscriptions) {
2200
- for (var id in subscriptions) {
2201
- if (subscriptions.hasOwnProperty(id)) {
2202
- var subscription = subscriptions[id];
2203
- if (subscription && !subscription.listener) {
2204
- delete subscriptions[id];
2205
- _cometd._debug('Removed failed subscription', subscription);
2206
- }
2207
- }
2208
- }
2209
- }
2210
- _handleCallback(message);
2211
- _notifyListeners('/meta/subscribe', message);
2212
- _notifyListeners('/meta/unsuccessful', message);
2213
- }
2214
-
2215
- function _subscribeResponse(message) {
2216
- if (message.successful) {
2217
- _handleCallback(message);
2218
- _notifyListeners('/meta/subscribe', message);
2219
- } else {
2220
- _failSubscribe(message);
2221
- }
2222
- }
2223
-
2224
- function _subscribeFailure(message) {
2225
- _failSubscribe(message);
2226
- }
2227
-
2228
- function _failUnsubscribe(message) {
2229
- _handleCallback(message);
2230
- _notifyListeners('/meta/unsubscribe', message);
2231
- _notifyListeners('/meta/unsuccessful', message);
2232
- }
2233
-
2234
- function _unsubscribeResponse(message) {
2235
- if (message.successful) {
2236
- _handleCallback(message);
2237
- _notifyListeners('/meta/unsubscribe', message);
2238
- } else {
2239
- _failUnsubscribe(message);
2240
- }
2241
- }
2242
-
2243
- function _unsubscribeFailure(message) {
2244
- _failUnsubscribe(message);
2245
- }
2246
-
2247
- function _failMessage(message) {
2248
- if (!_handleRemoteCall(message)) {
2249
- _handleCallback(message);
2250
- _notifyListeners('/meta/publish', message);
2251
- _notifyListeners('/meta/unsuccessful', message);
2252
- }
2253
- }
2254
-
2255
- function _messageResponse(message) {
2256
- if (message.data !== undefined) {
2257
- if (!_handleRemoteCall(message)) {
2258
- _notifyListeners(message.channel, message);
2259
- if (_handshakeMessages > 0) {
2260
- --_handshakeMessages;
2261
- if (_handshakeMessages === 0) {
2262
- _cometd._debug('Processed last handshake-delivered message');
2263
- _delayedConnect(0);
2264
- }
2265
- }
2266
- }
2267
- } else {
2268
- if (message.successful === undefined) {
2269
- _cometd._warn('Unknown Bayeux Message', message);
2270
- } else {
2271
- if (message.successful) {
2272
- _handleCallback(message);
2273
- _notifyListeners('/meta/publish', message);
2274
- } else {
2275
- _failMessage(message);
2276
- }
2277
- }
2278
- }
2279
- }
2280
-
2281
- function _messageFailure(failure) {
2282
- _failMessage(failure);
2283
- }
2284
-
2285
- function _receive(message) {
2286
- _unconnectTime = 0;
2287
-
2288
- message = _applyIncomingExtensions(message);
2289
- if (message === undefined || message === null) {
2290
- return;
2291
- }
2292
-
2293
- _updateAdvice(message.advice);
2294
-
2295
- var channel = message.channel;
2296
- switch (channel) {
2297
- case '/meta/handshake':
2298
- _handshakeResponse(message);
2299
- break;
2300
- case '/meta/connect':
2301
- _connectResponse(message);
2302
- break;
2303
- case '/meta/disconnect':
2304
- _disconnectResponse(message);
2305
- break;
2306
- case '/meta/subscribe':
2307
- _subscribeResponse(message);
2308
- break;
2309
- case '/meta/unsubscribe':
2310
- _unsubscribeResponse(message);
2311
- break;
2312
- default:
2313
- _messageResponse(message);
2314
- break;
2315
- }
2316
- }
2317
-
2318
- /**
2319
- * Receives a message.
2320
- * This method is exposed as a public so that extensions may inject
2321
- * messages simulating that they had been received.
2322
- */
2323
- this.receive = _receive;
2324
-
2325
- _handleMessages = function(rcvdMessages) {
2326
- _cometd._debug('Received', rcvdMessages);
2327
-
2328
- for (var i = 0; i < rcvdMessages.length; ++i) {
2329
- var message = rcvdMessages[i];
2330
- _receive(message);
2331
- }
2332
- };
2333
-
2334
- _handleFailure = function(conduit, messages, failure) {
2335
- _cometd._debug('handleFailure', conduit, messages, failure);
2336
-
2337
- failure.transport = conduit;
2338
- for (var i = 0; i < messages.length; ++i) {
2339
- var message = messages[i];
2340
- var failureMessage = {
2341
- id: message.id,
2342
- successful: false,
2343
- channel: message.channel,
2344
- failure: failure
2345
- };
2346
- failure.message = message;
2347
- switch (message.channel) {
2348
- case '/meta/handshake':
2349
- _handshakeFailure(failureMessage);
2350
- break;
2351
- case '/meta/connect':
2352
- _connectFailure(failureMessage);
2353
- break;
2354
- case '/meta/disconnect':
2355
- _disconnectFailure(failureMessage);
2356
- break;
2357
- case '/meta/subscribe':
2358
- failureMessage.subscription = message.subscription;
2359
- _subscribeFailure(failureMessage);
2360
- break;
2361
- case '/meta/unsubscribe':
2362
- failureMessage.subscription = message.subscription;
2363
- _unsubscribeFailure(failureMessage);
2364
- break;
2365
- default:
2366
- _messageFailure(failureMessage);
2367
- break;
2368
- }
2369
- }
2370
- };
2371
-
2372
- function _hasSubscriptions(channel) {
2373
- var subscriptions = _listeners[channel];
2374
- if (subscriptions) {
2375
- for (var id in subscriptions) {
2376
- if (subscriptions.hasOwnProperty(id)) {
2377
- if (subscriptions[id]) {
2378
- return true;
2379
- }
2380
- }
2381
- }
2382
- }
2383
- return false;
2384
- }
2385
-
2386
- function _resolveScopedCallback(scope, callback) {
2387
- var delegate = {
2388
- scope: scope,
2389
- method: callback
2390
- };
2391
- if (_isFunction(scope)) {
2392
- delegate.scope = undefined;
2393
- delegate.method = scope;
2394
- } else {
2395
- if (_isString(callback)) {
2396
- if (!scope) {
2397
- throw 'Invalid scope ' + scope;
2398
- }
2399
- delegate.method = scope[callback];
2400
- if (!_isFunction(delegate.method)) {
2401
- throw 'Invalid callback ' + callback + ' for scope ' + scope;
2402
- }
2403
- } else if (!_isFunction(callback)) {
2404
- throw 'Invalid callback ' + callback;
2405
- }
2406
- }
2407
- return delegate;
2408
- }
2409
-
2410
- function _addListener(channel, scope, callback, isListener) {
2411
- // The data structure is a map<channel, subscription[]>, where each subscription
2412
- // holds the callback to be called and its scope.
2413
-
2414
- var delegate = _resolveScopedCallback(scope, callback);
2415
- _cometd._debug('Adding', isListener ? 'listener' : 'subscription', 'on', channel, 'with scope', delegate.scope, 'and callback', delegate.method);
2416
-
2417
- var id = ++_listenerId;
2418
- var subscription = {
2419
- id: id,
2420
- channel: channel,
2421
- scope: delegate.scope,
2422
- callback: delegate.method,
2423
- listener: isListener
2424
- };
2425
-
2426
- var subscriptions = _listeners[channel];
2427
- if (!subscriptions) {
2428
- subscriptions = {};
2429
- _listeners[channel] = subscriptions;
2430
- }
2431
-
2432
- subscriptions[id] = subscription;
2433
-
2434
- _cometd._debug('Added', isListener ? 'listener' : 'subscription', subscription);
2435
-
2436
- return subscription;
2437
- }
2438
-
2439
- //
2440
- // PUBLIC API
2441
- //
2442
-
2443
- /**
2444
- * Registers the given transport under the given transport type.
2445
- * The optional index parameter specifies the "priority" at which the
2446
- * transport is registered (where 0 is the max priority).
2447
- * If a transport with the same type is already registered, this function
2448
- * does nothing and returns false.
2449
- * @param type the transport type
2450
- * @param transport the transport object
2451
- * @param index the index at which this transport is to be registered
2452
- * @return true if the transport has been registered, false otherwise
2453
- * @see #unregisterTransport(type)
2454
- */
2455
- this.registerTransport = function(type, transport, index) {
2456
- var result = _transports.add(type, transport, index);
2457
- if (result) {
2458
- this._debug('Registered transport', type);
2459
-
2460
- if (_isFunction(transport.registered)) {
2461
- transport.registered(type, this);
2462
- }
2463
- }
2464
- return result;
2465
- };
2466
-
2467
- /**
2468
- * Unregisters the transport with the given transport type.
2469
- * @param type the transport type to unregister
2470
- * @return the transport that has been unregistered,
2471
- * or null if no transport was previously registered under the given transport type
2472
- */
2473
- this.unregisterTransport = function(type) {
2474
- var transport = _transports.remove(type);
2475
- if (transport !== null) {
2476
- this._debug('Unregistered transport', type);
2477
-
2478
- if (_isFunction(transport.unregistered)) {
2479
- transport.unregistered();
2480
- }
2481
- }
2482
- return transport;
2483
- };
2484
-
2485
- this.unregisterTransports = function() {
2486
- _transports.clear();
2487
- };
2488
-
2489
- /**
2490
- * @return an array of all registered transport types
2491
- */
2492
- this.getTransportTypes = function() {
2493
- return _transports.getTransportTypes();
2494
- };
2495
-
2496
- this.findTransport = function(name) {
2497
- return _transports.find(name);
2498
- };
2499
-
2500
- /**
2501
- * @returns the TransportRegistry object
2502
- */
2503
- this.getTransportRegistry = function() {
2504
- return _transports;
2505
- };
2506
-
2507
- /**
2508
- * Configures the initial Bayeux communication with the Bayeux server.
2509
- * Configuration is passed via an object that must contain a mandatory field <code>url</code>
2510
- * of type string containing the URL of the Bayeux server.
2511
- * @param configuration the configuration object
2512
- */
2513
- this.configure = function(configuration) {
2514
- _configure.call(this, configuration);
2515
- };
2516
-
2517
- /**
2518
- * Configures and establishes the Bayeux communication with the Bayeux server
2519
- * via a handshake and a subsequent connect.
2520
- * @param configuration the configuration object
2521
- * @param handshakeProps an object to be merged with the handshake message
2522
- * @see #configure(configuration)
2523
- * @see #handshake(handshakeProps)
2524
- */
2525
- this.init = function(configuration, handshakeProps) {
2526
- this.configure(configuration);
2527
- this.handshake(handshakeProps);
2528
- };
2529
-
2530
- /**
2531
- * Establishes the Bayeux communication with the Bayeux server
2532
- * via a handshake and a subsequent connect.
2533
- * @param handshakeProps an object to be merged with the handshake message
2534
- * @param handshakeCallback a function to be invoked when the handshake is acknowledged
2535
- */
2536
- this.handshake = function(handshakeProps, handshakeCallback) {
2537
- if (_status !== 'disconnected') {
2538
- throw 'Illegal state: handshaken';
2539
- }
2540
- _handshake(handshakeProps, handshakeCallback);
2541
- };
2542
-
2543
- /**
2544
- * Disconnects from the Bayeux server.
2545
- * It is possible to suggest to attempt a synchronous disconnect, but this feature
2546
- * may only be available in certain transports (for example, long-polling may support
2547
- * it, callback-polling certainly does not).
2548
- * @param sync whether attempt to perform a synchronous disconnect
2549
- * @param disconnectProps an object to be merged with the disconnect message
2550
- * @param disconnectCallback a function to be invoked when the disconnect is acknowledged
2551
- */
2552
- this.disconnect = function(sync, disconnectProps, disconnectCallback) {
2553
- if (_isDisconnected()) {
2554
- return;
2555
- }
2556
-
2557
- if (typeof sync !== 'boolean') {
2558
- disconnectCallback = disconnectProps;
2559
- disconnectProps = sync;
2560
- sync = false;
2561
- }
2562
- if (_isFunction(disconnectProps)) {
2563
- disconnectCallback = disconnectProps;
2564
- disconnectProps = undefined;
2565
- }
2566
-
2567
- var bayeuxMessage = {
2568
- id: _nextMessageId(),
2569
- channel: '/meta/disconnect'
2570
- };
2571
- // Do not allow the user to override important fields.
2572
- var message = this._mixin(false, {}, disconnectProps, bayeuxMessage);
2573
-
2574
- // Save the callback.
2575
- _cometd._putCallback(message.id, disconnectCallback);
2576
-
2577
- _setStatus('disconnecting');
2578
- _send(sync === true, [message], false, 'disconnect');
2579
- };
2580
-
2581
- /**
2582
- * Marks the start of a batch of application messages to be sent to the server
2583
- * in a single request, obtaining a single response containing (possibly) many
2584
- * application reply messages.
2585
- * Messages are held in a queue and not sent until {@link #endBatch()} is called.
2586
- * If startBatch() is called multiple times, then an equal number of endBatch()
2587
- * calls must be made to close and send the batch of messages.
2588
- * @see #endBatch()
2589
- */
2590
- this.startBatch = function() {
2591
- _startBatch();
2592
- };
2593
-
2594
- /**
2595
- * Marks the end of a batch of application messages to be sent to the server
2596
- * in a single request.
2597
- * @see #startBatch()
2598
- */
2599
- this.endBatch = function() {
2600
- _endBatch();
2601
- };
2602
-
2603
- /**
2604
- * Executes the given callback in the given scope, surrounded by a {@link #startBatch()}
2605
- * and {@link #endBatch()} calls.
2606
- * @param scope the scope of the callback, may be omitted
2607
- * @param callback the callback to be executed within {@link #startBatch()} and {@link #endBatch()} calls
2608
- */
2609
- this.batch = function(scope, callback) {
2610
- var delegate = _resolveScopedCallback(scope, callback);
2611
- this.startBatch();
2612
- try {
2613
- delegate.method.call(delegate.scope);
2614
- this.endBatch();
2615
- } catch (x) {
2616
- this._info('Exception during execution of batch', x);
2617
- this.endBatch();
2618
- throw x;
2619
- }
2620
- };
2621
-
2622
- /**
2623
- * Adds a listener for bayeux messages, performing the given callback in the given scope
2624
- * when a message for the given channel arrives.
2625
- * @param channel the channel the listener is interested to
2626
- * @param scope the scope of the callback, may be omitted
2627
- * @param callback the callback to call when a message is sent to the channel
2628
- * @returns the subscription handle to be passed to {@link #removeListener(object)}
2629
- * @see #removeListener(subscription)
2630
- */
2631
- this.addListener = function(channel, scope, callback) {
2632
- if (arguments.length < 2) {
2633
- throw 'Illegal arguments number: required 2, got ' + arguments.length;
2634
- }
2635
- if (!_isString(channel)) {
2636
- throw 'Illegal argument type: channel must be a string';
2637
- }
2638
-
2639
- return _addListener(channel, scope, callback, true);
2640
- };
2641
-
2642
- /**
2643
- * Removes the subscription obtained with a call to {@link #addListener(string, object, function)}.
2644
- * @param subscription the subscription to unsubscribe.
2645
- * @see #addListener(channel, scope, callback)
2646
- */
2647
- this.removeListener = function(subscription) {
2648
- // Beware of subscription.id == 0, which is falsy => cannot use !subscription.id
2649
- if (!subscription || !subscription.channel || !("id" in subscription)) {
2650
- throw 'Invalid argument: expected subscription, not ' + subscription;
2651
- }
2652
-
2653
- _removeListener(subscription);
2654
- };
2655
-
2656
- /**
2657
- * Removes all listeners registered with {@link #addListener(channel, scope, callback)} or
2658
- * {@link #subscribe(channel, scope, callback)}.
2659
- */
2660
- this.clearListeners = function() {
2661
- _listeners = {};
2662
- };
2663
-
2664
- /**
2665
- * Subscribes to the given channel, performing the given callback in the given scope
2666
- * when a message for the channel arrives.
2667
- * @param channel the channel to subscribe to
2668
- * @param scope the scope of the callback, may be omitted
2669
- * @param callback the callback to call when a message is sent to the channel
2670
- * @param subscribeProps an object to be merged with the subscribe message
2671
- * @param subscribeCallback a function to be invoked when the subscription is acknowledged
2672
- * @return the subscription handle to be passed to {@link #unsubscribe(object)}
2673
- */
2674
- this.subscribe = function(channel, scope, callback, subscribeProps, subscribeCallback) {
2675
- if (arguments.length < 2) {
2676
- throw 'Illegal arguments number: required 2, got ' + arguments.length;
2677
- }
2678
- if (!_isString(channel)) {
2679
- throw 'Illegal argument type: channel must be a string';
2680
- }
2681
- if (_isDisconnected()) {
2682
- throw 'Illegal state: disconnected';
2683
- }
2684
-
2685
- // Normalize arguments
2686
- if (_isFunction(scope)) {
2687
- subscribeCallback = subscribeProps;
2688
- subscribeProps = callback;
2689
- callback = scope;
2690
- scope = undefined;
2691
- }
2692
- if (_isFunction(subscribeProps)) {
2693
- subscribeCallback = subscribeProps;
2694
- subscribeProps = undefined;
2695
- }
2696
-
2697
- // Only send the message to the server if this client has not yet subscribed to the channel
2698
- var send = !_hasSubscriptions(channel);
2699
-
2700
- var subscription = _addListener(channel, scope, callback, false);
2701
-
2702
- if (send) {
2703
- // Send the subscription message after the subscription registration to avoid
2704
- // races where the server would send a message to the subscribers, but here
2705
- // on the client the subscription has not been added yet to the data structures
2706
- var bayeuxMessage = {
2707
- id: _nextMessageId(),
2708
- channel: '/meta/subscribe',
2709
- subscription: channel
2710
- };
2711
- // Do not allow the user to override important fields.
2712
- var message = this._mixin(false, {}, subscribeProps, bayeuxMessage);
2713
-
2714
- // Save the callback.
2715
- _cometd._putCallback(message.id, subscribeCallback);
2716
-
2717
- _queueSend(message);
2718
- }
2719
-
2720
- return subscription;
2721
- };
2722
-
2723
- /**
2724
- * Unsubscribes the subscription obtained with a call to {@link #subscribe(string, object, function)}.
2725
- * @param subscription the subscription to unsubscribe.
2726
- * @param unsubscribeProps an object to be merged with the unsubscribe message
2727
- * @param unsubscribeCallback a function to be invoked when the unsubscription is acknowledged
2728
- */
2729
- this.unsubscribe = function(subscription, unsubscribeProps, unsubscribeCallback) {
2730
- if (arguments.length < 1) {
2731
- throw 'Illegal arguments number: required 1, got ' + arguments.length;
2732
- }
2733
- if (_isDisconnected()) {
2734
- throw 'Illegal state: disconnected';
2735
- }
2736
-
2737
- if (_isFunction(unsubscribeProps)) {
2738
- unsubscribeCallback = unsubscribeProps;
2739
- unsubscribeProps = undefined;
2740
- }
2741
-
2742
- // Remove the local listener before sending the message
2743
- // This ensures that if the server fails, this client does not get notifications
2744
- this.removeListener(subscription);
2745
-
2746
- var channel = subscription.channel;
2747
- // Only send the message to the server if this client unsubscribes the last subscription
2748
- if (!_hasSubscriptions(channel)) {
2749
- var bayeuxMessage = {
2750
- id: _nextMessageId(),
2751
- channel: '/meta/unsubscribe',
2752
- subscription: channel
2753
- };
2754
- // Do not allow the user to override important fields.
2755
- var message = this._mixin(false, {}, unsubscribeProps, bayeuxMessage);
2756
-
2757
- // Save the callback.
2758
- _cometd._putCallback(message.id, unsubscribeCallback);
2759
-
2760
- _queueSend(message);
2761
- }
2762
- };
2763
-
2764
- this.resubscribe = function(subscription, subscribeProps) {
2765
- _removeSubscription(subscription);
2766
- if (subscription) {
2767
- return this.subscribe(subscription.channel, subscription.scope, subscription.callback, subscribeProps);
2768
- }
2769
- return undefined;
2770
- };
2771
-
2772
- /**
2773
- * Removes all subscriptions added via {@link #subscribe(channel, scope, callback, subscribeProps)},
2774
- * but does not remove the listeners added via {@link addListener(channel, scope, callback)}.
2775
- */
2776
- this.clearSubscriptions = function() {
2777
- _clearSubscriptions();
2778
- };
2779
-
2780
- /**
2781
- * Publishes a message on the given channel, containing the given content.
2782
- * @param channel the channel to publish the message to
2783
- * @param content the content of the message
2784
- * @param publishProps an object to be merged with the publish message
2785
- * @param publishCallback a function to be invoked when the publish is acknowledged by the server
2786
- */
2787
- this.publish = function(channel, content, publishProps, publishCallback) {
2788
- if (arguments.length < 1) {
2789
- throw 'Illegal arguments number: required 1, got ' + arguments.length;
2790
- }
2791
- if (!_isString(channel)) {
2792
- throw 'Illegal argument type: channel must be a string';
2793
- }
2794
- if (/^\/meta\//.test(channel)) {
2795
- throw 'Illegal argument: cannot publish to meta channels';
2796
- }
2797
- if (_isDisconnected()) {
2798
- throw 'Illegal state: disconnected';
2799
- }
2800
-
2801
- if (_isFunction(content)) {
2802
- publishCallback = content;
2803
- content = {};
2804
- publishProps = undefined;
2805
- } else if (_isFunction(publishProps)) {
2806
- publishCallback = publishProps;
2807
- publishProps = undefined;
2808
- }
2809
-
2810
- var bayeuxMessage = {
2811
- id: _nextMessageId(),
2812
- channel: channel,
2813
- data: content
2814
- };
2815
- // Do not allow the user to override important fields.
2816
- var message = this._mixin(false, {}, publishProps, bayeuxMessage);
2817
-
2818
- // Save the callback.
2819
- _cometd._putCallback(message.id, publishCallback);
2820
-
2821
- _queueSend(message);
2822
- };
2823
-
2824
- /**
2825
- * Publishes a message with binary data on the given channel.
2826
- * The binary data chunk may be an ArrayBuffer, a DataView, a TypedArray
2827
- * (such as Uint8Array) or a plain integer array.
2828
- * The meta data object may contain additional application data such as
2829
- * a file name, a mime type, etc.
2830
- * @param channel the channel to publish the message to
2831
- * @param data the binary data to publish
2832
- * @param last whether the binary data chunk is the last
2833
- * @param meta an object containing meta data associated to the binary chunk
2834
- * @param callback a function to be invoked when the publish is acknowledged by the server
2835
- */
2836
- this.publishBinary = function(channel, data, last, meta, callback) {
2837
- if (_isFunction(data)) {
2838
- callback = data;
2839
- data = new ArrayBuffer(0);
2840
- last = true;
2841
- meta = undefined;
2842
- } else if (_isFunction(last)) {
2843
- callback = last;
2844
- last = true;
2845
- meta = undefined;
2846
- } else if (_isFunction(meta)) {
2847
- callback = meta;
2848
- meta = undefined;
2849
- }
2850
- var content = {
2851
- meta: meta,
2852
- data: data,
2853
- last: last
2854
- };
2855
- var ext = {
2856
- ext: {
2857
- binary: {
2858
- }
2859
- }
2860
- };
2861
- this.publish(channel, content, ext, callback);
2862
- };
2863
-
2864
- this.remoteCall = function(target, content, timeout, callProps, callback) {
2865
- if (arguments.length < 1) {
2866
- throw 'Illegal arguments number: required 1, got ' + arguments.length;
2867
- }
2868
- if (!_isString(target)) {
2869
- throw 'Illegal argument type: target must be a string';
2870
- }
2871
- if (_isDisconnected()) {
2872
- throw 'Illegal state: disconnected';
2873
- }
2874
-
2875
- if (_isFunction(content)) {
2876
- callback = content;
2877
- content = {};
2878
- timeout = _config.maxNetworkDelay;
2879
- callProps = undefined;
2880
- } else if (_isFunction(timeout)) {
2881
- callback = timeout;
2882
- timeout = _config.maxNetworkDelay;
2883
- callProps = undefined;
2884
- } else if (_isFunction(callProps)) {
2885
- callback = callProps;
2886
- callProps = undefined;
2887
- }
2888
-
2889
- if (typeof timeout !== 'number') {
2890
- throw 'Illegal argument type: timeout must be a number';
2891
- }
2892
-
2893
- if (!target.match(/^\//)) {
2894
- target = '/' + target;
2895
- }
2896
- var channel = '/service' + target;
2897
-
2898
- var bayeuxMessage = {
2899
- id: _nextMessageId(),
2900
- channel: channel,
2901
- data: content
2902
- };
2903
- var message = this._mixin(false, {}, callProps, bayeuxMessage);
2904
-
2905
- var context = {
2906
- callback: callback
2907
- };
2908
- if (timeout > 0) {
2909
- context.timeout = Utils.setTimeout(_cometd, function() {
2910
- _cometd._debug('Timing out remote call', message, 'after', timeout, 'ms');
2911
- _failMessage({
2912
- id: message.id,
2913
- error: '406::timeout',
2914
- successful: false,
2915
- failure: {
2916
- message : message,
2917
- reason: 'Remote Call Timeout'
2918
- }
2919
- });
2920
- }, timeout);
2921
- _cometd._debug('Scheduled remote call timeout', message, 'in', timeout, 'ms');
2922
- }
2923
- _remoteCalls[message.id] = context;
2924
-
2925
- _queueSend(message);
2926
- };
2927
-
2928
- this.remoteCallBinary = function(target, data, last, meta, timeout, callback) {
2929
- if (_isFunction(data)) {
2930
- callback = data;
2931
- data = new ArrayBuffer(0);
2932
- last = true;
2933
- meta = undefined;
2934
- timeout = _config.maxNetworkDelay;
2935
- } else if (_isFunction(last)) {
2936
- callback = last;
2937
- last = true;
2938
- meta = undefined;
2939
- timeout = _config.maxNetworkDelay;
2940
- } else if (_isFunction(meta)) {
2941
- callback = meta;
2942
- meta = undefined;
2943
- timeout = _config.maxNetworkDelay;
2944
- } else if (_isFunction(timeout)) {
2945
- callback = timeout;
2946
- timeout = _config.maxNetworkDelay;
2947
- }
2948
-
2949
- var content = {
2950
- meta: meta,
2951
- data: data,
2952
- last: last
2953
- };
2954
- var ext = {
2955
- ext: {
2956
- binary: {
2957
- }
2958
- }
2959
- };
2960
-
2961
- this.remoteCall(target, content, timeout, ext, callback);
2962
- };
2963
-
2964
- /**
2965
- * Returns a string representing the status of the bayeux communication with the Bayeux server.
2966
- */
2967
- this.getStatus = function() {
2968
- return _status;
2969
- };
2970
-
2971
- /**
2972
- * Returns whether this instance has been disconnected.
2973
- */
2974
- this.isDisconnected = _isDisconnected;
2975
-
2976
- /**
2977
- * Sets the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
2978
- * Default value is 1 second, which means if there is a persistent failure the retries will happen
2979
- * after 1 second, then after 2 seconds, then after 3 seconds, etc. So for example with 15 seconds of
2980
- * elapsed time, there will be 5 retries (at 1, 3, 6, 10 and 15 seconds elapsed).
2981
- * @param period the backoff period to set
2982
- * @see #getBackoffIncrement()
2983
- */
2984
- this.setBackoffIncrement = function(period) {
2985
- _config.backoffIncrement = period;
2986
- };
2987
-
2988
- /**
2989
- * Returns the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
2990
- * @see #setBackoffIncrement(period)
2991
- */
2992
- this.getBackoffIncrement = function() {
2993
- return _config.backoffIncrement;
2994
- };
2995
-
2996
- /**
2997
- * Returns the backoff period to wait before retrying an unsuccessful or failed message.
2998
- */
2999
- this.getBackoffPeriod = function() {
3000
- return _backoff;
3001
- };
3002
-
3003
- /**
3004
- * Increases the backoff period up to the maximum value configured.
3005
- * @returns the backoff period after increment
3006
- * @see getBackoffIncrement
3007
- */
3008
- this.increaseBackoffPeriod = function() {
3009
- return _increaseBackoff();
3010
- };
3011
-
3012
- /**
3013
- * Resets the backoff period to zero.
3014
- */
3015
- this.resetBackoffPeriod = function() {
3016
- _resetBackoff();
3017
- };
3018
-
3019
- /**
3020
- * Sets the log level for console logging.
3021
- * Valid values are the strings 'error', 'warn', 'info' and 'debug', from
3022
- * less verbose to more verbose.
3023
- * @param level the log level string
3024
- */
3025
- this.setLogLevel = function(level) {
3026
- _config.logLevel = level;
3027
- };
3028
-
3029
- /**
3030
- * Registers an extension whose callbacks are called for every incoming message
3031
- * (that comes from the server to this client implementation) and for every
3032
- * outgoing message (that originates from this client implementation for the
3033
- * server).
3034
- * The format of the extension object is the following:
3035
- * <pre>
3036
- * {
3037
- * incoming: function(message) { ... },
3038
- * outgoing: function(message) { ... }
3039
- * }
3040
- * </pre>
3041
- * Both properties are optional, but if they are present they will be called
3042
- * respectively for each incoming message and for each outgoing message.
3043
- * @param name the name of the extension
3044
- * @param extension the extension to register
3045
- * @return true if the extension was registered, false otherwise
3046
- * @see #unregisterExtension(name)
3047
- */
3048
- this.registerExtension = function(name, extension) {
3049
- if (arguments.length < 2) {
3050
- throw 'Illegal arguments number: required 2, got ' + arguments.length;
3051
- }
3052
- if (!_isString(name)) {
3053
- throw 'Illegal argument type: extension name must be a string';
3054
- }
3055
-
3056
- var existing = false;
3057
- for (var i = 0; i < _extensions.length; ++i) {
3058
- var existingExtension = _extensions[i];
3059
- if (existingExtension.name === name) {
3060
- existing = true;
3061
- break;
3062
- }
3063
- }
3064
- if (!existing) {
3065
- _extensions.push({
3066
- name: name,
3067
- extension: extension
3068
- });
3069
- this._debug('Registered extension', name);
3070
-
3071
- // Callback for extensions
3072
- if (_isFunction(extension.registered)) {
3073
- extension.registered(name, this);
3074
- }
3075
-
3076
- return true;
3077
- } else {
3078
- this._info('Could not register extension with name', name, 'since another extension with the same name already exists');
3079
- return false;
3080
- }
3081
- };
3082
-
3083
- /**
3084
- * Unregister an extension previously registered with
3085
- * {@link #registerExtension(name, extension)}.
3086
- * @param name the name of the extension to unregister.
3087
- * @return true if the extension was unregistered, false otherwise
3088
- */
3089
- this.unregisterExtension = function(name) {
3090
- if (!_isString(name)) {
3091
- throw 'Illegal argument type: extension name must be a string';
3092
- }
3093
-
3094
- var unregistered = false;
3095
- for (var i = 0; i < _extensions.length; ++i) {
3096
- var extension = _extensions[i];
3097
- if (extension.name === name) {
3098
- _extensions.splice(i, 1);
3099
- unregistered = true;
3100
- this._debug('Unregistered extension', name);
3101
-
3102
- // Callback for extensions
3103
- var ext = extension.extension;
3104
- if (_isFunction(ext.unregistered)) {
3105
- ext.unregistered();
3106
- }
3107
-
3108
- break;
3109
- }
3110
- }
3111
- return unregistered;
3112
- };
3113
-
3114
- /**
3115
- * Find the extension registered with the given name.
3116
- * @param name the name of the extension to find
3117
- * @return the extension found or null if no extension with the given name has been registered
3118
- */
3119
- this.getExtension = function(name) {
3120
- for (var i = 0; i < _extensions.length; ++i) {
3121
- var extension = _extensions[i];
3122
- if (extension.name === name) {
3123
- return extension.extension;
3124
- }
3125
- }
3126
- return null;
3127
- };
3128
-
3129
- /**
3130
- * Returns the name assigned to this CometD object, or the string 'default'
3131
- * if no name has been explicitly passed as parameter to the constructor.
3132
- */
3133
- this.getName = function() {
3134
- return _name;
3135
- };
3136
-
3137
- /**
3138
- * Returns the clientId assigned by the Bayeux server during handshake.
3139
- */
3140
- this.getClientId = function() {
3141
- return _clientId;
3142
- };
3143
-
3144
- /**
3145
- * Returns the URL of the Bayeux server.
3146
- */
3147
- this.getURL = function() {
3148
- if (_transport) {
3149
- var url = _transport.getURL();
3150
- if (url) {
3151
- return url;
3152
- }
3153
- url = _config.urls[_transport.getType()];
3154
- if (url) {
3155
- return url;
3156
- }
3157
- }
3158
- return _config.url;
3159
- };
3160
-
3161
- this.getTransport = function() {
3162
- return _transport;
3163
- };
3164
-
3165
- this.getConfiguration = function() {
3166
- return this._mixin(true, {}, _config);
3167
- };
3168
-
3169
- this.getAdvice = function() {
3170
- return this._mixin(true, {}, _advice);
3171
- };
3172
-
3173
- // Initialize transports.
3174
- if (window.WebSocket) {
3175
- this.registerTransport('websocket', new WebSocketTransport());
3176
- }
3177
- this.registerTransport('long-polling', new LongPollingTransport());
3178
- this.registerTransport('callback-polling', new CallbackPollingTransport());
3179
- };
3180
-
3181
- var _z85EncodeTable = [
3182
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
3183
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
3184
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
3185
- 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
3186
- 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
3187
- 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
3188
- 'Y', 'Z', '.', '-', ':', '+', '=', '^', '!', '/',
3189
- '*', '?', '&', '<', '>', '(', ')', '[', ']', '{',
3190
- '}', '@', '%', '$', '#'
3191
- ];
3192
- var _z85DecodeTable = [
3193
- 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
3194
- 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
3195
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3196
- 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
3197
- 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
3198
- 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
3199
- 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
3200
- 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
3201
- 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
3202
- 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
3203
- 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
3204
- 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
3205
- ];
3206
- var Z85 = {
3207
- encode: function(bytes) {
3208
- var buffer = null;
3209
- if (bytes instanceof ArrayBuffer) {
3210
- buffer = bytes;
3211
- } else if (bytes.buffer instanceof ArrayBuffer) {
3212
- buffer = bytes.buffer;
3213
- } else if (Array.isArray(bytes)) {
3214
- buffer = new Uint8Array(bytes).buffer;
3215
- }
3216
- if (buffer == null) {
3217
- throw 'Cannot Z85 encode ' + bytes;
3218
- }
3219
-
3220
- var length = buffer.byteLength;
3221
- var remainder = length % 4;
3222
- var padding = 4 - (remainder === 0 ? 4 : remainder);
3223
- var view = new DataView(buffer);
3224
- var result = '';
3225
- var value = 0;
3226
- for (var i = 0; i < length + padding; ++i) {
3227
- var isPadding = i >= length;
3228
- value = value * 256 + (isPadding ? 0 : view.getUint8(i));
3229
- if ((i + 1) % 4 === 0) {
3230
- var divisor = 85 * 85 * 85 * 85;
3231
- for (var j = 5; j > 0; --j) {
3232
- if (!isPadding || j > padding) {
3233
- var code = Math.floor(value / divisor) % 85;
3234
- result += _z85EncodeTable[code];
3235
- }
3236
- divisor /= 85;
3237
- }
3238
- value = 0;
3239
- }
3240
- }
3241
-
3242
- return result;
3243
- },
3244
- decode: function(string) {
3245
- var remainder = string.length % 5;
3246
- var padding = 5 - (remainder === 0 ? 5 : remainder);
3247
- for (var p = 0; p < padding; ++p) {
3248
- string += _z85EncodeTable[_z85EncodeTable.length - 1];
3249
- }
3250
- var length = string.length;
3251
-
3252
- var buffer = new ArrayBuffer((length * 4 / 5) - padding);
3253
- var view = new DataView(buffer);
3254
- var value = 0;
3255
- var charIdx = 0;
3256
- var byteIdx = 0;
3257
- for (var i = 0; i < length; ++i) {
3258
- var code = string.charCodeAt(charIdx++) - 32;
3259
- value = value * 85 + _z85DecodeTable[code];
3260
- if (charIdx % 5 === 0) {
3261
- var divisor = 256 * 256 * 256;
3262
- while (divisor >= 1) {
3263
- if (byteIdx < view.byteLength) {
3264
- view.setUint8(byteIdx++, Math.floor(value / divisor) % 256);
3265
- }
3266
- divisor /= 256;
3267
- }
3268
- value = 0;
3269
- }
3270
- }
3271
-
3272
- return buffer;
3273
- }
3274
- };
3275
-
3276
- return {
3277
- CometD: CometD,
3278
- Transport: Transport,
3279
- RequestTransport: RequestTransport,
3280
- LongPollingTransport: LongPollingTransport,
3281
- CallbackPollingTransport: CallbackPollingTransport,
3282
- WebSocketTransport: WebSocketTransport,
3283
- Utils: Utils,
3284
- Z85: Z85
3285
- };
3286
- }));