centrifuge 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/README.md +144 -12
- data/lib/centrifuge/client.rb +19 -6
- data/lib/centrifuge/version.rb +1 -1
- data/lib/centrifuge.rb +2 -2
- data/spec/client_spec.rb +2 -2
- data/vendor/assets/javascripts/centrifuge.js +1 -1562
- data/vendor/assets/javascripts/sockjs.js +3 -2379
- metadata +3 -2
@@ -1,1562 +1 @@
|
|
1
|
-
// v0.7.0
|
2
|
-
;(function () {
|
3
|
-
'use strict';
|
4
|
-
|
5
|
-
/**
|
6
|
-
* Oliver Caldwell
|
7
|
-
* http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/
|
8
|
-
*/
|
9
|
-
|
10
|
-
if (!Object.create) {
|
11
|
-
Object.create = (function(){
|
12
|
-
function F(){}
|
13
|
-
|
14
|
-
return function(o){
|
15
|
-
if (arguments.length != 1) {
|
16
|
-
throw new Error('Object.create implementation only accepts one parameter.');
|
17
|
-
}
|
18
|
-
F.prototype = o;
|
19
|
-
return new F()
|
20
|
-
}
|
21
|
-
})()
|
22
|
-
}
|
23
|
-
|
24
|
-
if (!Array.prototype.indexOf) {
|
25
|
-
Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
|
26
|
-
'use strict';
|
27
|
-
if (this == null) {
|
28
|
-
throw new TypeError();
|
29
|
-
}
|
30
|
-
var n, k, t = Object(this),
|
31
|
-
len = t.length >>> 0;
|
32
|
-
|
33
|
-
if (len === 0) {
|
34
|
-
return -1;
|
35
|
-
}
|
36
|
-
n = 0;
|
37
|
-
if (arguments.length > 1) {
|
38
|
-
n = Number(arguments[1]);
|
39
|
-
if (n != n) { // shortcut for verifying if it's NaN
|
40
|
-
n = 0;
|
41
|
-
} else if (n != 0 && n != Infinity && n != -Infinity) {
|
42
|
-
n = (n > 0 || -1) * Math.floor(Math.abs(n));
|
43
|
-
}
|
44
|
-
}
|
45
|
-
if (n >= len) {
|
46
|
-
return -1;
|
47
|
-
}
|
48
|
-
for (k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); k < len; k++) {
|
49
|
-
if (k in t && t[k] === searchElement) {
|
50
|
-
return k;
|
51
|
-
}
|
52
|
-
}
|
53
|
-
return -1;
|
54
|
-
};
|
55
|
-
}
|
56
|
-
|
57
|
-
function extend(destination, source) {
|
58
|
-
destination.prototype = Object.create(source.prototype);
|
59
|
-
destination.prototype.constructor = destination;
|
60
|
-
return source.prototype;
|
61
|
-
}
|
62
|
-
|
63
|
-
/**
|
64
|
-
* EventEmitter v4.2.3 - git.io/ee
|
65
|
-
* Oliver Caldwell
|
66
|
-
* MIT license
|
67
|
-
* @preserve
|
68
|
-
*/
|
69
|
-
|
70
|
-
/**
|
71
|
-
* Class for managing events.
|
72
|
-
* Can be extended to provide event functionality in other classes.
|
73
|
-
*
|
74
|
-
* @class EventEmitter Manages event registering and emitting.
|
75
|
-
*/
|
76
|
-
function EventEmitter() {}
|
77
|
-
|
78
|
-
// Shortcuts to improve speed and size
|
79
|
-
|
80
|
-
// Easy access to the prototype
|
81
|
-
var proto = EventEmitter.prototype;
|
82
|
-
|
83
|
-
/**
|
84
|
-
* Finds the index of the listener for the event in it's storage array.
|
85
|
-
*
|
86
|
-
* @param {Function[]} listeners Array of listeners to search through.
|
87
|
-
* @param {Function} listener Method to look for.
|
88
|
-
* @return {Number} Index of the specified listener, -1 if not found
|
89
|
-
* @api private
|
90
|
-
*/
|
91
|
-
function indexOfListener(listeners, listener) {
|
92
|
-
var i = listeners.length;
|
93
|
-
while (i--) {
|
94
|
-
if (listeners[i].listener === listener) {
|
95
|
-
return i;
|
96
|
-
}
|
97
|
-
}
|
98
|
-
|
99
|
-
return -1;
|
100
|
-
}
|
101
|
-
|
102
|
-
/**
|
103
|
-
* Alias a method while keeping the context correct, to allow for overwriting of target method.
|
104
|
-
*
|
105
|
-
* @param {String} name The name of the target method.
|
106
|
-
* @return {Function} The aliased method
|
107
|
-
* @api private
|
108
|
-
*/
|
109
|
-
function alias(name) {
|
110
|
-
return function aliasClosure() {
|
111
|
-
return this[name].apply(this, arguments);
|
112
|
-
};
|
113
|
-
}
|
114
|
-
|
115
|
-
/**
|
116
|
-
* Returns the listener array for the specified event.
|
117
|
-
* Will initialise the event object and listener arrays if required.
|
118
|
-
* Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
|
119
|
-
* Each property in the object response is an array of listener functions.
|
120
|
-
*
|
121
|
-
* @param {String|RegExp} evt Name of the event to return the listeners from.
|
122
|
-
* @return {Function[]|Object} All listener functions for the event.
|
123
|
-
*/
|
124
|
-
proto.getListeners = function getListeners(evt) {
|
125
|
-
var events = this._getEvents();
|
126
|
-
var response;
|
127
|
-
var key;
|
128
|
-
|
129
|
-
// Return a concatenated array of all matching events if
|
130
|
-
// the selector is a regular expression.
|
131
|
-
if (typeof evt === 'object') {
|
132
|
-
response = {};
|
133
|
-
for (key in events) {
|
134
|
-
if (events.hasOwnProperty(key) && evt.test(key)) {
|
135
|
-
response[key] = events[key];
|
136
|
-
}
|
137
|
-
}
|
138
|
-
}
|
139
|
-
else {
|
140
|
-
response = events[evt] || (events[evt] = []);
|
141
|
-
}
|
142
|
-
|
143
|
-
return response;
|
144
|
-
};
|
145
|
-
|
146
|
-
/**
|
147
|
-
* Takes a list of listener objects and flattens it into a list of listener functions.
|
148
|
-
*
|
149
|
-
* @param {Object[]} listeners Raw listener objects.
|
150
|
-
* @return {Function[]} Just the listener functions.
|
151
|
-
*/
|
152
|
-
proto.flattenListeners = function flattenListeners(listeners) {
|
153
|
-
var flatListeners = [];
|
154
|
-
var i;
|
155
|
-
|
156
|
-
for (i = 0; i < listeners.length; i += 1) {
|
157
|
-
flatListeners.push(listeners[i].listener);
|
158
|
-
}
|
159
|
-
|
160
|
-
return flatListeners;
|
161
|
-
};
|
162
|
-
|
163
|
-
/**
|
164
|
-
* Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
|
165
|
-
*
|
166
|
-
* @param {String|RegExp} evt Name of the event to return the listeners from.
|
167
|
-
* @return {Object} All listener functions for an event in an object.
|
168
|
-
*/
|
169
|
-
proto.getListenersAsObject = function getListenersAsObject(evt) {
|
170
|
-
var listeners = this.getListeners(evt);
|
171
|
-
var response;
|
172
|
-
|
173
|
-
if (listeners instanceof Array) {
|
174
|
-
response = {};
|
175
|
-
response[evt] = listeners;
|
176
|
-
}
|
177
|
-
|
178
|
-
return response || listeners;
|
179
|
-
};
|
180
|
-
|
181
|
-
/**
|
182
|
-
* Adds a listener function to the specified event.
|
183
|
-
* The listener will not be added if it is a duplicate.
|
184
|
-
* If the listener returns true then it will be removed after it is called.
|
185
|
-
* If you pass a regular expression as the event name then the listener will be added to all events that match it.
|
186
|
-
*
|
187
|
-
* @param {String|RegExp} evt Name of the event to attach the listener to.
|
188
|
-
* @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
|
189
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
190
|
-
*/
|
191
|
-
proto.addListener = function addListener(evt, listener) {
|
192
|
-
var listeners = this.getListenersAsObject(evt);
|
193
|
-
var listenerIsWrapped = typeof listener === 'object';
|
194
|
-
var key;
|
195
|
-
|
196
|
-
for (key in listeners) {
|
197
|
-
if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
|
198
|
-
listeners[key].push(listenerIsWrapped ? listener : {
|
199
|
-
listener: listener,
|
200
|
-
once: false
|
201
|
-
});
|
202
|
-
}
|
203
|
-
}
|
204
|
-
|
205
|
-
return this;
|
206
|
-
};
|
207
|
-
|
208
|
-
/**
|
209
|
-
* Alias of addListener
|
210
|
-
*/
|
211
|
-
proto.on = alias('addListener');
|
212
|
-
|
213
|
-
/**
|
214
|
-
* Semi-alias of addListener. It will add a listener that will be
|
215
|
-
* automatically removed after it's first execution.
|
216
|
-
*
|
217
|
-
* @param {String|RegExp} evt Name of the event to attach the listener to.
|
218
|
-
* @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
|
219
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
220
|
-
*/
|
221
|
-
proto.addOnceListener = function addOnceListener(evt, listener) {
|
222
|
-
//noinspection JSValidateTypes
|
223
|
-
return this.addListener(evt, {
|
224
|
-
listener: listener,
|
225
|
-
once: true
|
226
|
-
});
|
227
|
-
};
|
228
|
-
|
229
|
-
/**
|
230
|
-
* Alias of addOnceListener.
|
231
|
-
*/
|
232
|
-
proto.once = alias('addOnceListener');
|
233
|
-
|
234
|
-
/**
|
235
|
-
* Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
|
236
|
-
* You need to tell it what event names should be matched by a regex.
|
237
|
-
*
|
238
|
-
* @param {String} evt Name of the event to create.
|
239
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
240
|
-
*/
|
241
|
-
proto.defineEvent = function defineEvent(evt) {
|
242
|
-
this.getListeners(evt);
|
243
|
-
return this;
|
244
|
-
};
|
245
|
-
|
246
|
-
/**
|
247
|
-
* Uses defineEvent to define multiple events.
|
248
|
-
*
|
249
|
-
* @param {String[]} evts An array of event names to define.
|
250
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
251
|
-
*/
|
252
|
-
proto.defineEvents = function defineEvents(evts) {
|
253
|
-
for (var i = 0; i < evts.length; i += 1) {
|
254
|
-
this.defineEvent(evts[i]);
|
255
|
-
}
|
256
|
-
return this;
|
257
|
-
};
|
258
|
-
|
259
|
-
/**
|
260
|
-
* Removes a listener function from the specified event.
|
261
|
-
* When passed a regular expression as the event name, it will remove the listener from all events that match it.
|
262
|
-
*
|
263
|
-
* @param {String|RegExp} evt Name of the event to remove the listener from.
|
264
|
-
* @param {Function} listener Method to remove from the event.
|
265
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
266
|
-
*/
|
267
|
-
proto.removeListener = function removeListener(evt, listener) {
|
268
|
-
var listeners = this.getListenersAsObject(evt);
|
269
|
-
var index;
|
270
|
-
var key;
|
271
|
-
|
272
|
-
for (key in listeners) {
|
273
|
-
if (listeners.hasOwnProperty(key)) {
|
274
|
-
index = indexOfListener(listeners[key], listener);
|
275
|
-
|
276
|
-
if (index !== -1) {
|
277
|
-
listeners[key].splice(index, 1);
|
278
|
-
}
|
279
|
-
}
|
280
|
-
}
|
281
|
-
|
282
|
-
return this;
|
283
|
-
};
|
284
|
-
|
285
|
-
/**
|
286
|
-
* Alias of removeListener
|
287
|
-
*/
|
288
|
-
proto.off = alias('removeListener');
|
289
|
-
|
290
|
-
/**
|
291
|
-
* Adds listeners in bulk using the manipulateListeners method.
|
292
|
-
* If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
|
293
|
-
* You can also pass it a regular expression to add the array of listeners to all events that match it.
|
294
|
-
* Yeah, this function does quite a bit. That's probably a bad thing.
|
295
|
-
*
|
296
|
-
* @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
|
297
|
-
* @param {Function[]} [listeners] An optional array of listener functions to add.
|
298
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
299
|
-
*/
|
300
|
-
proto.addListeners = function addListeners(evt, listeners) {
|
301
|
-
// Pass through to manipulateListeners
|
302
|
-
return this.manipulateListeners(false, evt, listeners);
|
303
|
-
};
|
304
|
-
|
305
|
-
/**
|
306
|
-
* Removes listeners in bulk using the manipulateListeners method.
|
307
|
-
* If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
|
308
|
-
* You can also pass it an event name and an array of listeners to be removed.
|
309
|
-
* You can also pass it a regular expression to remove the listeners from all events that match it.
|
310
|
-
*
|
311
|
-
* @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
|
312
|
-
* @param {Function[]} [listeners] An optional array of listener functions to remove.
|
313
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
314
|
-
*/
|
315
|
-
proto.removeListeners = function removeListeners(evt, listeners) {
|
316
|
-
// Pass through to manipulateListeners
|
317
|
-
return this.manipulateListeners(true, evt, listeners);
|
318
|
-
};
|
319
|
-
|
320
|
-
/**
|
321
|
-
* Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
|
322
|
-
* The first argument will determine if the listeners are removed (true) or added (false).
|
323
|
-
* If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
|
324
|
-
* You can also pass it an event name and an array of listeners to be added/removed.
|
325
|
-
* You can also pass it a regular expression to manipulate the listeners of all events that match it.
|
326
|
-
*
|
327
|
-
* @param {Boolean} remove True if you want to remove listeners, false if you want to add.
|
328
|
-
* @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
|
329
|
-
* @param {Function[]} [listeners] An optional array of listener functions to add/remove.
|
330
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
331
|
-
*/
|
332
|
-
proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
|
333
|
-
var i;
|
334
|
-
var value;
|
335
|
-
var single = remove ? this.removeListener : this.addListener;
|
336
|
-
var multiple = remove ? this.removeListeners : this.addListeners;
|
337
|
-
|
338
|
-
// If evt is an object then pass each of it's properties to this method
|
339
|
-
if (typeof evt === 'object' && !(evt instanceof RegExp)) {
|
340
|
-
for (i in evt) {
|
341
|
-
if (evt.hasOwnProperty(i) && (value = evt[i])) {
|
342
|
-
// Pass the single listener straight through to the singular method
|
343
|
-
if (typeof value === 'function') {
|
344
|
-
single.call(this, i, value);
|
345
|
-
}
|
346
|
-
else {
|
347
|
-
// Otherwise pass back to the multiple function
|
348
|
-
multiple.call(this, i, value);
|
349
|
-
}
|
350
|
-
}
|
351
|
-
}
|
352
|
-
}
|
353
|
-
else {
|
354
|
-
// So evt must be a string
|
355
|
-
// And listeners must be an array of listeners
|
356
|
-
// Loop over it and pass each one to the multiple method
|
357
|
-
i = listeners.length;
|
358
|
-
while (i--) {
|
359
|
-
single.call(this, evt, listeners[i]);
|
360
|
-
}
|
361
|
-
}
|
362
|
-
|
363
|
-
return this;
|
364
|
-
};
|
365
|
-
|
366
|
-
/**
|
367
|
-
* Removes all listeners from a specified event.
|
368
|
-
* If you do not specify an event then all listeners will be removed.
|
369
|
-
* That means every event will be emptied.
|
370
|
-
* You can also pass a regex to remove all events that match it.
|
371
|
-
*
|
372
|
-
* @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
|
373
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
374
|
-
*/
|
375
|
-
proto.removeEvent = function removeEvent(evt) {
|
376
|
-
var type = typeof evt;
|
377
|
-
var events = this._getEvents();
|
378
|
-
var key;
|
379
|
-
|
380
|
-
// Remove different things depending on the state of evt
|
381
|
-
if (type === 'string') {
|
382
|
-
// Remove all listeners for the specified event
|
383
|
-
delete events[evt];
|
384
|
-
}
|
385
|
-
else if (type === 'object') {
|
386
|
-
// Remove all events matching the regex.
|
387
|
-
for (key in events) {
|
388
|
-
//noinspection JSUnresolvedFunction
|
389
|
-
if (events.hasOwnProperty(key) && evt.test(key)) {
|
390
|
-
delete events[key];
|
391
|
-
}
|
392
|
-
}
|
393
|
-
}
|
394
|
-
else {
|
395
|
-
// Remove all listeners in all events
|
396
|
-
delete this._events;
|
397
|
-
}
|
398
|
-
|
399
|
-
return this;
|
400
|
-
};
|
401
|
-
|
402
|
-
/**
|
403
|
-
* Emits an event of your choice.
|
404
|
-
* When emitted, every listener attached to that event will be executed.
|
405
|
-
* If you pass the optional argument array then those arguments will be passed to every listener upon execution.
|
406
|
-
* Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
|
407
|
-
* So they will not arrive within the array on the other side, they will be separate.
|
408
|
-
* You can also pass a regular expression to emit to all events that match it.
|
409
|
-
*
|
410
|
-
* @param {String|RegExp} evt Name of the event to emit and execute listeners for.
|
411
|
-
* @param {Array} [args] Optional array of arguments to be passed to each listener.
|
412
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
413
|
-
*/
|
414
|
-
proto.emitEvent = function emitEvent(evt, args) {
|
415
|
-
var listeners = this.getListenersAsObject(evt);
|
416
|
-
var listener;
|
417
|
-
var i;
|
418
|
-
var key;
|
419
|
-
var response;
|
420
|
-
|
421
|
-
for (key in listeners) {
|
422
|
-
if (listeners.hasOwnProperty(key)) {
|
423
|
-
i = listeners[key].length;
|
424
|
-
|
425
|
-
while (i--) {
|
426
|
-
// If the listener returns true then it shall be removed from the event
|
427
|
-
// The function is executed either with a basic call or an apply if there is an args array
|
428
|
-
listener = listeners[key][i];
|
429
|
-
|
430
|
-
if (listener.once === true) {
|
431
|
-
this.removeListener(evt, listener.listener);
|
432
|
-
}
|
433
|
-
|
434
|
-
response = listener.listener.apply(this, args || []);
|
435
|
-
|
436
|
-
if (response === this._getOnceReturnValue()) {
|
437
|
-
this.removeListener(evt, listener.listener);
|
438
|
-
}
|
439
|
-
}
|
440
|
-
}
|
441
|
-
}
|
442
|
-
|
443
|
-
return this;
|
444
|
-
};
|
445
|
-
|
446
|
-
/**
|
447
|
-
* Alias of emitEvent
|
448
|
-
*/
|
449
|
-
proto.trigger = alias('emitEvent');
|
450
|
-
|
451
|
-
//noinspection JSValidateJSDoc,JSCommentMatchesSignature
|
452
|
-
/**
|
453
|
-
* Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
|
454
|
-
* As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
|
455
|
-
*
|
456
|
-
* @param {String|RegExp} evt Name of the event to emit and execute listeners for.
|
457
|
-
* @param {...*} Optional additional arguments to be passed to each listener.
|
458
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
459
|
-
*/
|
460
|
-
proto.emit = function emit(evt) {
|
461
|
-
var args = Array.prototype.slice.call(arguments, 1);
|
462
|
-
return this.emitEvent(evt, args);
|
463
|
-
};
|
464
|
-
|
465
|
-
/**
|
466
|
-
* Sets the current value to check against when executing listeners. If a
|
467
|
-
* listeners return value matches the one set here then it will be removed
|
468
|
-
* after execution. This value defaults to true.
|
469
|
-
*
|
470
|
-
* @param {*} value The new value to check for when executing listeners.
|
471
|
-
* @return {Object} Current instance of EventEmitter for chaining.
|
472
|
-
*/
|
473
|
-
proto.setOnceReturnValue = function setOnceReturnValue(value) {
|
474
|
-
this._onceReturnValue = value;
|
475
|
-
return this;
|
476
|
-
};
|
477
|
-
|
478
|
-
/**
|
479
|
-
* Fetches the current value to check against when executing listeners. If
|
480
|
-
* the listeners return value matches this one then it should be removed
|
481
|
-
* automatically. It will return true by default.
|
482
|
-
*
|
483
|
-
* @return {*|Boolean} The current value to check for or the default, true.
|
484
|
-
* @api private
|
485
|
-
*/
|
486
|
-
proto._getOnceReturnValue = function _getOnceReturnValue() {
|
487
|
-
if (this.hasOwnProperty('_onceReturnValue')) {
|
488
|
-
return this._onceReturnValue;
|
489
|
-
}
|
490
|
-
else {
|
491
|
-
return true;
|
492
|
-
}
|
493
|
-
};
|
494
|
-
|
495
|
-
/**
|
496
|
-
* Fetches the events object and creates one if required.
|
497
|
-
*
|
498
|
-
* @return {Object} The events storage object.
|
499
|
-
* @api private
|
500
|
-
*/
|
501
|
-
proto._getEvents = function _getEvents() {
|
502
|
-
return this._events || (this._events = {});
|
503
|
-
};
|
504
|
-
|
505
|
-
/**
|
506
|
-
* Mixes in the given objects into the target object by copying the properties.
|
507
|
-
* @param deep if the copy must be deep
|
508
|
-
* @param target the target object
|
509
|
-
* @param objects the objects whose properties are copied into the target
|
510
|
-
*/
|
511
|
-
function mixin(deep, target, objects) {
|
512
|
-
var result = target || {};
|
513
|
-
|
514
|
-
// Skip first 2 parameters (deep and target), and loop over the others
|
515
|
-
for (var i = 2; i < arguments.length; ++i) {
|
516
|
-
var object = arguments[i];
|
517
|
-
|
518
|
-
if (object === undefined || object === null) {
|
519
|
-
continue;
|
520
|
-
}
|
521
|
-
|
522
|
-
for (var propName in object) {
|
523
|
-
//noinspection JSUnfilteredForInLoop
|
524
|
-
var prop = fieldValue(object, propName);
|
525
|
-
//noinspection JSUnfilteredForInLoop
|
526
|
-
var targ = fieldValue(result, propName);
|
527
|
-
|
528
|
-
// Avoid infinite loops
|
529
|
-
if (prop === target) {
|
530
|
-
continue;
|
531
|
-
}
|
532
|
-
// Do not mixin undefined values
|
533
|
-
if (prop === undefined) {
|
534
|
-
continue;
|
535
|
-
}
|
536
|
-
|
537
|
-
if (deep && typeof prop === 'object' && prop !== null) {
|
538
|
-
if (prop instanceof Array) {
|
539
|
-
//noinspection JSUnfilteredForInLoop
|
540
|
-
result[propName] = mixin(deep, targ instanceof Array ? targ : [], prop);
|
541
|
-
} else {
|
542
|
-
var source = typeof targ === 'object' && !(targ instanceof Array) ? targ : {};
|
543
|
-
//noinspection JSUnfilteredForInLoop
|
544
|
-
result[propName] = mixin(deep, source, prop);
|
545
|
-
}
|
546
|
-
} else {
|
547
|
-
//noinspection JSUnfilteredForInLoop
|
548
|
-
result[propName] = prop;
|
549
|
-
}
|
550
|
-
}
|
551
|
-
}
|
552
|
-
|
553
|
-
return result;
|
554
|
-
}
|
555
|
-
|
556
|
-
function fieldValue(object, name) {
|
557
|
-
try {
|
558
|
-
return object[name];
|
559
|
-
} catch (x) {
|
560
|
-
return undefined;
|
561
|
-
}
|
562
|
-
}
|
563
|
-
|
564
|
-
// http://krasimirtsonev.com/blog/article/Cross-browser-handling-of-Ajax-requests-in-absurdjs
|
565
|
-
var AJAX = {
|
566
|
-
request: function(url, method, ops) {
|
567
|
-
ops.data = ops.data || {};
|
568
|
-
|
569
|
-
var getParams = function(data, request_url) {
|
570
|
-
var arr = [], str;
|
571
|
-
for(var name in data) {
|
572
|
-
if (typeof data[name] === 'object') {
|
573
|
-
for (var i in data[name]) {
|
574
|
-
arr.push(name + '=' + encodeURIComponent(data[name][i]));
|
575
|
-
}
|
576
|
-
} else {
|
577
|
-
arr.push(name + '=' + encodeURIComponent(data[name]));
|
578
|
-
}
|
579
|
-
}
|
580
|
-
str = arr.join('&');
|
581
|
-
if(str != '') {
|
582
|
-
return request_url ? (request_url.indexOf('?') < 0 ? '?' + str : '&' + str) : str;
|
583
|
-
}
|
584
|
-
return '';
|
585
|
-
};
|
586
|
-
var api = {
|
587
|
-
host: {},
|
588
|
-
setHeaders: function(headers) {
|
589
|
-
for(var name in headers) {
|
590
|
-
this.xhr && this.xhr.setRequestHeader(name, headers[name]);
|
591
|
-
}
|
592
|
-
},
|
593
|
-
process: function(ops) {
|
594
|
-
var self = this;
|
595
|
-
this.xhr = null;
|
596
|
-
if (window.ActiveXObject) {
|
597
|
-
this.xhr = new ActiveXObject('Microsoft.XMLHTTP');
|
598
|
-
}
|
599
|
-
else if (window.XMLHttpRequest) {
|
600
|
-
this.xhr = new XMLHttpRequest();
|
601
|
-
}
|
602
|
-
if(this.xhr) {
|
603
|
-
this.xhr.onreadystatechange = function() {
|
604
|
-
if(self.xhr.readyState == 4 && self.xhr.status == 200) {
|
605
|
-
var result = self.xhr.responseText;
|
606
|
-
if (typeof JSON != 'undefined') {
|
607
|
-
result = JSON.parse(result);
|
608
|
-
} else {
|
609
|
-
throw "JSON undefined";
|
610
|
-
}
|
611
|
-
self.doneCallback && self.doneCallback.apply(self.host, [result, self.xhr]);
|
612
|
-
} else if(self.xhr.readyState == 4) {
|
613
|
-
self.failCallback && self.failCallback.apply(self.host, [self.xhr]);
|
614
|
-
}
|
615
|
-
self.alwaysCallback && self.alwaysCallback.apply(self.host, [self.xhr]);
|
616
|
-
}
|
617
|
-
}
|
618
|
-
if(method == 'get') {
|
619
|
-
this.xhr.open("GET", url + getParams(ops.data, url), true);
|
620
|
-
} else {
|
621
|
-
this.xhr.open(method, url, true);
|
622
|
-
this.setHeaders({
|
623
|
-
'X-Requested-With': 'XMLHttpRequest',
|
624
|
-
'Content-type': 'application/x-www-form-urlencoded'
|
625
|
-
});
|
626
|
-
}
|
627
|
-
if(ops.headers && typeof ops.headers == 'object') {
|
628
|
-
this.setHeaders(ops.headers);
|
629
|
-
}
|
630
|
-
setTimeout(function() {
|
631
|
-
method == 'get' ? self.xhr.send() : self.xhr.send(getParams(ops.data));
|
632
|
-
}, 20);
|
633
|
-
return this;
|
634
|
-
},
|
635
|
-
done: function(callback) {
|
636
|
-
this.doneCallback = callback;
|
637
|
-
return this;
|
638
|
-
},
|
639
|
-
fail: function(callback) {
|
640
|
-
this.failCallback = callback;
|
641
|
-
return this;
|
642
|
-
},
|
643
|
-
always: function(callback) {
|
644
|
-
this.alwaysCallback = callback;
|
645
|
-
return this;
|
646
|
-
}
|
647
|
-
};
|
648
|
-
return api.process(ops);
|
649
|
-
}
|
650
|
-
};
|
651
|
-
|
652
|
-
function endsWith(value, suffix) {
|
653
|
-
return value.indexOf(suffix, value.length - suffix.length) !== -1;
|
654
|
-
}
|
655
|
-
|
656
|
-
function startsWith(value, prefix) {
|
657
|
-
return value.lastIndexOf(prefix, 0) === 0;
|
658
|
-
}
|
659
|
-
|
660
|
-
function stripSlash(value) {
|
661
|
-
if (value.substring(value.length - 1) == "/") {
|
662
|
-
value = value.substring(0, value.length - 1);
|
663
|
-
}
|
664
|
-
return value;
|
665
|
-
}
|
666
|
-
|
667
|
-
function isString(value) {
|
668
|
-
if (value === undefined || value === null) {
|
669
|
-
return false;
|
670
|
-
}
|
671
|
-
return typeof value === 'string' || value instanceof String;
|
672
|
-
}
|
673
|
-
|
674
|
-
function isFunction(value) {
|
675
|
-
if (value === undefined || value === null) {
|
676
|
-
return false;
|
677
|
-
}
|
678
|
-
return typeof value === 'function';
|
679
|
-
}
|
680
|
-
|
681
|
-
function log(level, args) {
|
682
|
-
if (window.console) {
|
683
|
-
var logger = window.console[level];
|
684
|
-
if (isFunction(logger)) {
|
685
|
-
logger.apply(window.console, args);
|
686
|
-
}
|
687
|
-
}
|
688
|
-
}
|
689
|
-
|
690
|
-
function Centrifuge(options) {
|
691
|
-
this._sockjs = false;
|
692
|
-
this._status = 'disconnected';
|
693
|
-
this._reconnect = true;
|
694
|
-
this._transport = null;
|
695
|
-
this._messageId = 0;
|
696
|
-
this._clientId = null;
|
697
|
-
this._subscriptions = {};
|
698
|
-
this._messages = [];
|
699
|
-
this._isBatching = false;
|
700
|
-
this._isAuthBatching = false;
|
701
|
-
this._authChannels = {};
|
702
|
-
this._refreshTimeout = null;
|
703
|
-
this._config = {
|
704
|
-
retry: 3000,
|
705
|
-
info: null,
|
706
|
-
debug: false,
|
707
|
-
insecure: false,
|
708
|
-
server: null,
|
709
|
-
protocols_whitelist: [
|
710
|
-
'websocket',
|
711
|
-
'xdr-streaming',
|
712
|
-
'xhr-streaming',
|
713
|
-
'iframe-eventsource',
|
714
|
-
'iframe-htmlfile',
|
715
|
-
'xdr-polling',
|
716
|
-
'xhr-polling',
|
717
|
-
'iframe-xhr-polling',
|
718
|
-
'jsonp-polling'
|
719
|
-
],
|
720
|
-
privateChannelPrefix: "$",
|
721
|
-
refreshEndpoint: "/centrifuge/refresh",
|
722
|
-
authEndpoint: "/centrifuge/auth",
|
723
|
-
authHeaders: {},
|
724
|
-
refreshHeaders: {}
|
725
|
-
};
|
726
|
-
if (options) {
|
727
|
-
this.configure(options);
|
728
|
-
}
|
729
|
-
}
|
730
|
-
|
731
|
-
extend(Centrifuge, EventEmitter);
|
732
|
-
|
733
|
-
var centrifugeProto = Centrifuge.prototype;
|
734
|
-
|
735
|
-
centrifugeProto._debug = function () {
|
736
|
-
if (this._config.debug === true) {
|
737
|
-
log('debug', arguments);
|
738
|
-
}
|
739
|
-
};
|
740
|
-
|
741
|
-
centrifugeProto._configure = function (configuration) {
|
742
|
-
this._debug('Configuring centrifuge object with', configuration);
|
743
|
-
|
744
|
-
if (!configuration) {
|
745
|
-
configuration = {};
|
746
|
-
}
|
747
|
-
|
748
|
-
this._config = mixin(false, this._config, configuration);
|
749
|
-
|
750
|
-
if (!this._config.url) {
|
751
|
-
throw 'Missing required configuration parameter \'url\' specifying the Centrifuge server URL';
|
752
|
-
}
|
753
|
-
|
754
|
-
if (!this._config.project) {
|
755
|
-
throw 'Missing required configuration parameter \'project\' specifying project ID in Centrifuge';
|
756
|
-
}
|
757
|
-
|
758
|
-
if (!this._config.user && this._config.user !== '') {
|
759
|
-
if (!this._config.insecure) {
|
760
|
-
throw 'Missing required configuration parameter \'user\' specifying user\'s unique ID in your application';
|
761
|
-
} else {
|
762
|
-
this._debug("user not found but this is OK for insecure mode - anonymous access will be used");
|
763
|
-
this._config.user = "";
|
764
|
-
}
|
765
|
-
}
|
766
|
-
|
767
|
-
if (!this._config.timestamp) {
|
768
|
-
if (!this._config.insecure) {
|
769
|
-
throw 'Missing required configuration parameter \'timestamp\'';
|
770
|
-
} else {
|
771
|
-
this._debug("token not found but this is OK for insecure mode");
|
772
|
-
}
|
773
|
-
}
|
774
|
-
|
775
|
-
if (!this._config.token) {
|
776
|
-
if (!this._config.insecure) {
|
777
|
-
throw 'Missing required configuration parameter \'token\' specifying the sign of authorization request';
|
778
|
-
} else {
|
779
|
-
this._debug("timestamp not found but this is OK for insecure mode");
|
780
|
-
}
|
781
|
-
}
|
782
|
-
|
783
|
-
this._config.url = stripSlash(this._config.url);
|
784
|
-
|
785
|
-
if (endsWith(this._config.url, 'connection')) {
|
786
|
-
//noinspection JSUnresolvedVariable
|
787
|
-
if (typeof window.SockJS === 'undefined') {
|
788
|
-
throw 'You need to include SockJS client library before Centrifuge javascript client library or use pure Websocket connection endpoint';
|
789
|
-
}
|
790
|
-
this._sockjs = true;
|
791
|
-
}
|
792
|
-
};
|
793
|
-
|
794
|
-
centrifugeProto._setStatus = function (newStatus) {
|
795
|
-
if (this._status !== newStatus) {
|
796
|
-
this._debug('Status', this._status, '->', newStatus);
|
797
|
-
this._status = newStatus;
|
798
|
-
}
|
799
|
-
};
|
800
|
-
|
801
|
-
centrifugeProto._isDisconnected = function () {
|
802
|
-
return this._isConnected() === false;
|
803
|
-
};
|
804
|
-
|
805
|
-
centrifugeProto._isConnected = function () {
|
806
|
-
return this._status === 'connected';
|
807
|
-
};
|
808
|
-
|
809
|
-
centrifugeProto._isConnecting = function () {
|
810
|
-
return this._status === 'connecting';
|
811
|
-
};
|
812
|
-
|
813
|
-
centrifugeProto._nextMessageId = function () {
|
814
|
-
return ++this._messageId;
|
815
|
-
};
|
816
|
-
|
817
|
-
centrifugeProto._clearSubscriptions = function () {
|
818
|
-
this._subscriptions = {};
|
819
|
-
};
|
820
|
-
|
821
|
-
centrifugeProto._send = function (messages) {
|
822
|
-
// We must be sure that the messages have a clientId.
|
823
|
-
// This is not guaranteed since the handshake may take time to return
|
824
|
-
// (and hence the clientId is not known yet) and the application
|
825
|
-
// may create other messages.
|
826
|
-
for (var i = 0; i < messages.length; ++i) {
|
827
|
-
var message = messages[i];
|
828
|
-
message.uid = '' + this._nextMessageId();
|
829
|
-
|
830
|
-
if (this._clientId) {
|
831
|
-
message.clientId = this._clientId;
|
832
|
-
}
|
833
|
-
|
834
|
-
this._debug('Send', message);
|
835
|
-
this._transport.send(JSON.stringify(message));
|
836
|
-
}
|
837
|
-
};
|
838
|
-
|
839
|
-
centrifugeProto._connect = function (callback) {
|
840
|
-
|
841
|
-
if (this.isConnected()) {
|
842
|
-
return;
|
843
|
-
}
|
844
|
-
|
845
|
-
this._clientId = null;
|
846
|
-
|
847
|
-
this._reconnect = true;
|
848
|
-
|
849
|
-
this._clearSubscriptions();
|
850
|
-
|
851
|
-
this._setStatus('connecting');
|
852
|
-
|
853
|
-
var self = this;
|
854
|
-
|
855
|
-
if (callback) {
|
856
|
-
this.on('connect', callback);
|
857
|
-
}
|
858
|
-
|
859
|
-
if (this._sockjs === true) {
|
860
|
-
//noinspection JSUnresolvedFunction
|
861
|
-
var sockjs_options = {
|
862
|
-
protocols_whitelist: this._config.protocols_whitelist
|
863
|
-
};
|
864
|
-
if (this._config.server !== null) {
|
865
|
-
sockjs_options['server'] = this._config.server;
|
866
|
-
}
|
867
|
-
|
868
|
-
this._transport = new SockJS(this._config.url, null, sockjs_options);
|
869
|
-
|
870
|
-
} else {
|
871
|
-
this._transport = new WebSocket(this._config.url);
|
872
|
-
}
|
873
|
-
|
874
|
-
this._setStatus('connecting');
|
875
|
-
|
876
|
-
this._transport.onopen = function () {
|
877
|
-
|
878
|
-
var centrifugeMessage = {
|
879
|
-
'method': 'connect',
|
880
|
-
'params': {
|
881
|
-
'user': self._config.user,
|
882
|
-
'project': self._config.project
|
883
|
-
}
|
884
|
-
};
|
885
|
-
|
886
|
-
if (self._config.info !== null) {
|
887
|
-
centrifugeMessage["params"]["info"] = self._config.info;
|
888
|
-
}
|
889
|
-
|
890
|
-
if (!self._config.insecure) {
|
891
|
-
centrifugeMessage["params"]["timestamp"] = self._config.timestamp;
|
892
|
-
centrifugeMessage["params"]["token"] = self._config.token;
|
893
|
-
}
|
894
|
-
self.send(centrifugeMessage);
|
895
|
-
};
|
896
|
-
|
897
|
-
this._transport.onerror = function (error) {
|
898
|
-
self._debug(error);
|
899
|
-
};
|
900
|
-
|
901
|
-
this._transport.onclose = function () {
|
902
|
-
self._setStatus('disconnected');
|
903
|
-
self.trigger('disconnect');
|
904
|
-
if (self._reconnect === true) {
|
905
|
-
window.setTimeout(function () {
|
906
|
-
if (self._reconnect === true) {
|
907
|
-
self._connect.call(self);
|
908
|
-
}
|
909
|
-
}, self._config.retry);
|
910
|
-
}
|
911
|
-
};
|
912
|
-
|
913
|
-
this._transport.onmessage = function (event) {
|
914
|
-
var data;
|
915
|
-
data = JSON.parse(event.data);
|
916
|
-
self._debug('Received', data);
|
917
|
-
self._receive(data);
|
918
|
-
};
|
919
|
-
};
|
920
|
-
|
921
|
-
centrifugeProto._disconnect = function () {
|
922
|
-
this._clientId = null;
|
923
|
-
this._setStatus('disconnected');
|
924
|
-
this._subscriptions = {};
|
925
|
-
this._reconnect = false;
|
926
|
-
this._transport.close();
|
927
|
-
};
|
928
|
-
|
929
|
-
centrifugeProto._getSubscription = function (channel) {
|
930
|
-
var subscription;
|
931
|
-
subscription = this._subscriptions[channel];
|
932
|
-
if (!subscription) {
|
933
|
-
return null;
|
934
|
-
}
|
935
|
-
return subscription;
|
936
|
-
};
|
937
|
-
|
938
|
-
centrifugeProto._removeSubscription = function (channel) {
|
939
|
-
try {
|
940
|
-
delete this._subscriptions[channel];
|
941
|
-
} catch (e) {
|
942
|
-
this._debug('nothing to delete for channel ', channel);
|
943
|
-
}
|
944
|
-
try {
|
945
|
-
delete this._authChannels[channel];
|
946
|
-
} catch (e) {
|
947
|
-
this._debug('nothing to delete from authChannels for channel ', channel);
|
948
|
-
}
|
949
|
-
};
|
950
|
-
|
951
|
-
centrifugeProto._connectResponse = function (message) {
|
952
|
-
if (this.isConnected()) {
|
953
|
-
return;
|
954
|
-
}
|
955
|
-
if (message.error === null) {
|
956
|
-
if (!message.body) {
|
957
|
-
return;
|
958
|
-
}
|
959
|
-
var isExpired = message.body.expired;
|
960
|
-
if (isExpired) {
|
961
|
-
this.refresh();
|
962
|
-
return;
|
963
|
-
}
|
964
|
-
this._clientId = message.body.client;
|
965
|
-
this._setStatus('connected');
|
966
|
-
this.trigger('connect', [message]);
|
967
|
-
if (this._refreshTimeout) {
|
968
|
-
window.clearTimeout(this._refreshTimeout);
|
969
|
-
}
|
970
|
-
if (message.body.ttl !== null) {
|
971
|
-
var self = this;
|
972
|
-
this._refreshTimeout = window.setTimeout(function() {
|
973
|
-
self.refresh.call(self);
|
974
|
-
}, message.body.ttl * 1000);
|
975
|
-
}
|
976
|
-
} else {
|
977
|
-
this.trigger('error', [message]);
|
978
|
-
this.trigger('connect:error', [message]);
|
979
|
-
}
|
980
|
-
};
|
981
|
-
|
982
|
-
centrifugeProto._disconnectResponse = function (message) {
|
983
|
-
if (message.error === null) {
|
984
|
-
this.disconnect();
|
985
|
-
} else {
|
986
|
-
this.trigger('error', [message]);
|
987
|
-
this.trigger('disconnect:error', [message.error]);
|
988
|
-
}
|
989
|
-
};
|
990
|
-
|
991
|
-
centrifugeProto._subscribeResponse = function (message) {
|
992
|
-
if (message.error !== null) {
|
993
|
-
this.trigger('error', [message]);
|
994
|
-
}
|
995
|
-
var body = message.body;
|
996
|
-
if (body === null) {
|
997
|
-
return;
|
998
|
-
}
|
999
|
-
var channel = body.channel;
|
1000
|
-
var subscription = this.getSubscription(channel);
|
1001
|
-
if (!subscription) {
|
1002
|
-
return;
|
1003
|
-
}
|
1004
|
-
if (message.error === null) {
|
1005
|
-
subscription.trigger('subscribe:success', [body]);
|
1006
|
-
subscription.trigger('ready', [body]);
|
1007
|
-
} else {
|
1008
|
-
subscription.trigger('subscribe:error', [message.error]);
|
1009
|
-
subscription.trigger('error', [message]);
|
1010
|
-
}
|
1011
|
-
};
|
1012
|
-
|
1013
|
-
centrifugeProto._unsubscribeResponse = function (message) {
|
1014
|
-
var body = message.body;
|
1015
|
-
var channel = body.channel;
|
1016
|
-
var subscription = this.getSubscription(channel);
|
1017
|
-
if (!subscription) {
|
1018
|
-
return;
|
1019
|
-
}
|
1020
|
-
if (message.error === null) {
|
1021
|
-
subscription.trigger('unsubscribe', [body]);
|
1022
|
-
this._centrifuge._removeSubscription(channel);
|
1023
|
-
}
|
1024
|
-
};
|
1025
|
-
|
1026
|
-
centrifugeProto._publishResponse = function (message) {
|
1027
|
-
var body = message.body;
|
1028
|
-
var channel = body.channel;
|
1029
|
-
var subscription = this.getSubscription(channel);
|
1030
|
-
if (!subscription) {
|
1031
|
-
return;
|
1032
|
-
}
|
1033
|
-
if (message.error === null) {
|
1034
|
-
subscription.trigger('publish:success', [body]);
|
1035
|
-
} else {
|
1036
|
-
subscription.trigger('publish:error', [message.error]);
|
1037
|
-
this.trigger('error', [message]);
|
1038
|
-
}
|
1039
|
-
};
|
1040
|
-
|
1041
|
-
centrifugeProto._presenceResponse = function (message) {
|
1042
|
-
var body = message.body;
|
1043
|
-
var channel = body.channel;
|
1044
|
-
var subscription = this.getSubscription(channel);
|
1045
|
-
if (!subscription) {
|
1046
|
-
return;
|
1047
|
-
}
|
1048
|
-
if (message.error === null) {
|
1049
|
-
subscription.trigger('presence', [body]);
|
1050
|
-
subscription.trigger('presence:success', [body]);
|
1051
|
-
} else {
|
1052
|
-
subscription.trigger('presence:error', [message.error]);
|
1053
|
-
this.trigger('error', [message]);
|
1054
|
-
}
|
1055
|
-
};
|
1056
|
-
|
1057
|
-
centrifugeProto._historyResponse = function (message) {
|
1058
|
-
var body = message.body;
|
1059
|
-
var channel = body.channel;
|
1060
|
-
var subscription = this.getSubscription(channel);
|
1061
|
-
if (!subscription) {
|
1062
|
-
return;
|
1063
|
-
}
|
1064
|
-
if (message.error === null) {
|
1065
|
-
subscription.trigger('history', [body]);
|
1066
|
-
subscription.trigger('history:success', [body]);
|
1067
|
-
} else {
|
1068
|
-
subscription.trigger('history:error', [message.error]);
|
1069
|
-
this.trigger('error', [message]);
|
1070
|
-
}
|
1071
|
-
};
|
1072
|
-
|
1073
|
-
centrifugeProto._joinResponse = function(message) {
|
1074
|
-
var body = message.body;
|
1075
|
-
var channel = body.channel;
|
1076
|
-
var subscription = this.getSubscription(channel);
|
1077
|
-
if (!subscription) {
|
1078
|
-
return;
|
1079
|
-
}
|
1080
|
-
subscription.trigger('join', [body]);
|
1081
|
-
};
|
1082
|
-
|
1083
|
-
centrifugeProto._leaveResponse = function(message) {
|
1084
|
-
var body = message.body;
|
1085
|
-
var channel = body.channel;
|
1086
|
-
var subscription = this.getSubscription(channel);
|
1087
|
-
if (!subscription) {
|
1088
|
-
return;
|
1089
|
-
}
|
1090
|
-
subscription.trigger('leave', [body]);
|
1091
|
-
};
|
1092
|
-
|
1093
|
-
centrifugeProto._messageResponse = function (message) {
|
1094
|
-
var body = message.body;
|
1095
|
-
var channel = body.channel;
|
1096
|
-
var subscription = this.getSubscription(channel);
|
1097
|
-
if (subscription === null) {
|
1098
|
-
return;
|
1099
|
-
}
|
1100
|
-
subscription.trigger('message', [body]);
|
1101
|
-
};
|
1102
|
-
|
1103
|
-
centrifugeProto._refreshResponse = function (message) {
|
1104
|
-
if (this._refreshTimeout) {
|
1105
|
-
window.clearTimeout(this._refreshTimeout);
|
1106
|
-
}
|
1107
|
-
if (message.body.ttl !== null) {
|
1108
|
-
var self = this;
|
1109
|
-
self._refreshTimeout = window.setTimeout(function () {
|
1110
|
-
self.refresh.call(self);
|
1111
|
-
}, message.body.ttl * 1000);
|
1112
|
-
}
|
1113
|
-
};
|
1114
|
-
|
1115
|
-
centrifugeProto._dispatchMessage = function(message) {
|
1116
|
-
if (message === undefined || message === null) {
|
1117
|
-
return;
|
1118
|
-
}
|
1119
|
-
|
1120
|
-
var method = message.method;
|
1121
|
-
|
1122
|
-
if (!method) {
|
1123
|
-
return;
|
1124
|
-
}
|
1125
|
-
|
1126
|
-
switch (method) {
|
1127
|
-
case 'connect':
|
1128
|
-
this._connectResponse(message);
|
1129
|
-
break;
|
1130
|
-
case 'disconnect':
|
1131
|
-
this._disconnectResponse(message);
|
1132
|
-
break;
|
1133
|
-
case 'subscribe':
|
1134
|
-
this._subscribeResponse(message);
|
1135
|
-
break;
|
1136
|
-
case 'unsubscribe':
|
1137
|
-
this._unsubscribeResponse(message);
|
1138
|
-
break;
|
1139
|
-
case 'publish':
|
1140
|
-
this._publishResponse(message);
|
1141
|
-
break;
|
1142
|
-
case 'presence':
|
1143
|
-
this._presenceResponse(message);
|
1144
|
-
break;
|
1145
|
-
case 'history':
|
1146
|
-
this._historyResponse(message);
|
1147
|
-
break;
|
1148
|
-
case 'join':
|
1149
|
-
this._joinResponse(message);
|
1150
|
-
break;
|
1151
|
-
case 'leave':
|
1152
|
-
this._leaveResponse(message);
|
1153
|
-
break;
|
1154
|
-
case 'ping':
|
1155
|
-
break;
|
1156
|
-
case 'refresh':
|
1157
|
-
this._refreshResponse(message);
|
1158
|
-
break;
|
1159
|
-
case 'message':
|
1160
|
-
this._messageResponse(message);
|
1161
|
-
break;
|
1162
|
-
default:
|
1163
|
-
break;
|
1164
|
-
}
|
1165
|
-
};
|
1166
|
-
|
1167
|
-
centrifugeProto._receive = function (data) {
|
1168
|
-
if (Object.prototype.toString.call(data) === Object.prototype.toString.call([])) {
|
1169
|
-
for (var i in data) {
|
1170
|
-
if (data.hasOwnProperty(i)) {
|
1171
|
-
var msg = data[i];
|
1172
|
-
this._dispatchMessage(msg);
|
1173
|
-
}
|
1174
|
-
}
|
1175
|
-
} else if (Object.prototype.toString.call(data) === Object.prototype.toString.call({})) {
|
1176
|
-
this._dispatchMessage(data);
|
1177
|
-
}
|
1178
|
-
};
|
1179
|
-
|
1180
|
-
centrifugeProto._flush = function() {
|
1181
|
-
var messages = this._messages.slice(0);
|
1182
|
-
this._messages = [];
|
1183
|
-
this._send(messages);
|
1184
|
-
};
|
1185
|
-
|
1186
|
-
centrifugeProto._ping = function () {
|
1187
|
-
var centrifugeMessage = {
|
1188
|
-
"method": "ping",
|
1189
|
-
"params": {}
|
1190
|
-
};
|
1191
|
-
this.send(centrifugeMessage);
|
1192
|
-
};
|
1193
|
-
|
1194
|
-
/* PUBLIC API */
|
1195
|
-
|
1196
|
-
centrifugeProto.getClientId = function () {
|
1197
|
-
return this._clientId;
|
1198
|
-
};
|
1199
|
-
|
1200
|
-
centrifugeProto.isConnected = centrifugeProto._isConnected;
|
1201
|
-
|
1202
|
-
centrifugeProto.isConnecting = centrifugeProto._isConnecting;
|
1203
|
-
|
1204
|
-
centrifugeProto.isDisconnected = centrifugeProto._isDisconnected;
|
1205
|
-
|
1206
|
-
centrifugeProto.configure = function (configuration) {
|
1207
|
-
this._configure.call(this, configuration);
|
1208
|
-
};
|
1209
|
-
|
1210
|
-
centrifugeProto.connect = centrifugeProto._connect;
|
1211
|
-
|
1212
|
-
centrifugeProto.disconnect = centrifugeProto._disconnect;
|
1213
|
-
|
1214
|
-
centrifugeProto.getSubscription = centrifugeProto._getSubscription;
|
1215
|
-
|
1216
|
-
centrifugeProto.ping = centrifugeProto._ping;
|
1217
|
-
|
1218
|
-
centrifugeProto.send = function (message) {
|
1219
|
-
if (this._isBatching === true) {
|
1220
|
-
this._messages.push(message);
|
1221
|
-
} else {
|
1222
|
-
this._send([message]);
|
1223
|
-
}
|
1224
|
-
};
|
1225
|
-
|
1226
|
-
centrifugeProto.startBatching = function () {
|
1227
|
-
// start collecting messages without sending them to Centrifuge until flush
|
1228
|
-
// method called
|
1229
|
-
this._isBatching = true;
|
1230
|
-
};
|
1231
|
-
|
1232
|
-
centrifugeProto.stopBatching = function(flush) {
|
1233
|
-
// stop collecting messages
|
1234
|
-
flush = flush || false;
|
1235
|
-
this._isBatching = false;
|
1236
|
-
if (flush === true) {
|
1237
|
-
this.flush();
|
1238
|
-
}
|
1239
|
-
};
|
1240
|
-
|
1241
|
-
centrifugeProto.flush = function() {
|
1242
|
-
// send batched messages to Centrifuge
|
1243
|
-
this._flush();
|
1244
|
-
};
|
1245
|
-
|
1246
|
-
centrifugeProto.startAuthBatching = function() {
|
1247
|
-
// start collecting private channels to create bulk authentication
|
1248
|
-
// request to authEndpoint when stopAuthBatching will be called
|
1249
|
-
this._isAuthBatching = true;
|
1250
|
-
};
|
1251
|
-
|
1252
|
-
centrifugeProto.stopAuthBatching = function(callback) {
|
1253
|
-
// create request to authEndpoint with collected private channels
|
1254
|
-
// to ask if this client can subscribe on each channel
|
1255
|
-
this._isAuthBatching = false;
|
1256
|
-
var authChannels = this._authChannels;
|
1257
|
-
this._authChannels = {};
|
1258
|
-
var channels = [];
|
1259
|
-
|
1260
|
-
for (var channel in authChannels) {
|
1261
|
-
var subscription = this.getSubscription(channel);
|
1262
|
-
if (!subscription) {
|
1263
|
-
continue;
|
1264
|
-
}
|
1265
|
-
channels.push(channel);
|
1266
|
-
}
|
1267
|
-
|
1268
|
-
if (channels.length == 0) {
|
1269
|
-
if (callback) {
|
1270
|
-
callback();
|
1271
|
-
}
|
1272
|
-
return;
|
1273
|
-
}
|
1274
|
-
|
1275
|
-
var data = {
|
1276
|
-
"client": this.getClientId(),
|
1277
|
-
"channels": channels
|
1278
|
-
};
|
1279
|
-
|
1280
|
-
var self = this;
|
1281
|
-
|
1282
|
-
AJAX.request(this._config.authEndpoint, "post", {
|
1283
|
-
"headers": this._config.authHeaders,
|
1284
|
-
"data": data
|
1285
|
-
}).done(function(data) {
|
1286
|
-
for (var i in channels) {
|
1287
|
-
var channel = channels[i];
|
1288
|
-
var channelResponse = data[channel];
|
1289
|
-
if (!channelResponse) {
|
1290
|
-
// subscription:error
|
1291
|
-
self._subscribeResponse({
|
1292
|
-
"error": 404,
|
1293
|
-
"body": {
|
1294
|
-
"channel": channel
|
1295
|
-
}
|
1296
|
-
});
|
1297
|
-
continue;
|
1298
|
-
}
|
1299
|
-
if (!channelResponse.status || channelResponse.status === 200) {
|
1300
|
-
var centrifugeMessage = {
|
1301
|
-
"method": "subscribe",
|
1302
|
-
"params": {
|
1303
|
-
"channel": channel,
|
1304
|
-
"client": self.getClientId(),
|
1305
|
-
"info": channelResponse.info,
|
1306
|
-
"sign": channelResponse.sign
|
1307
|
-
}
|
1308
|
-
};
|
1309
|
-
self.send(centrifugeMessage);
|
1310
|
-
} else {
|
1311
|
-
self._subscribeResponse({
|
1312
|
-
"error": channelResponse.status,
|
1313
|
-
"body": {
|
1314
|
-
"channel": channel
|
1315
|
-
}
|
1316
|
-
});
|
1317
|
-
}
|
1318
|
-
}
|
1319
|
-
}).fail(function() {
|
1320
|
-
log("info", "authorization request failed");
|
1321
|
-
return false;
|
1322
|
-
}).always(function(){
|
1323
|
-
if (callback) {
|
1324
|
-
callback();
|
1325
|
-
}
|
1326
|
-
})
|
1327
|
-
|
1328
|
-
};
|
1329
|
-
|
1330
|
-
centrifugeProto.subscribe = function (channel, callback) {
|
1331
|
-
|
1332
|
-
if (arguments.length < 1) {
|
1333
|
-
throw 'Illegal arguments number: required 1, got ' + arguments.length;
|
1334
|
-
}
|
1335
|
-
if (!isString(channel)) {
|
1336
|
-
throw 'Illegal argument type: channel must be a string';
|
1337
|
-
}
|
1338
|
-
if (this.isDisconnected()) {
|
1339
|
-
throw 'Illegal state: already disconnected';
|
1340
|
-
}
|
1341
|
-
|
1342
|
-
var current_subscription = this.getSubscription(channel);
|
1343
|
-
|
1344
|
-
if (current_subscription !== null) {
|
1345
|
-
return current_subscription;
|
1346
|
-
} else {
|
1347
|
-
var subscription = new Subscription(this, channel);
|
1348
|
-
this._subscriptions[channel] = subscription;
|
1349
|
-
subscription.subscribe(callback);
|
1350
|
-
return subscription;
|
1351
|
-
}
|
1352
|
-
};
|
1353
|
-
|
1354
|
-
centrifugeProto.unsubscribe = function (channel) {
|
1355
|
-
if (arguments.length < 1) {
|
1356
|
-
throw 'Illegal arguments number: required 1, got ' + arguments.length;
|
1357
|
-
}
|
1358
|
-
if (!isString(channel)) {
|
1359
|
-
throw 'Illegal argument type: channel must be a string';
|
1360
|
-
}
|
1361
|
-
if (this.isDisconnected()) {
|
1362
|
-
return;
|
1363
|
-
}
|
1364
|
-
|
1365
|
-
var subscription = this.getSubscription(channel);
|
1366
|
-
if (subscription !== null) {
|
1367
|
-
subscription.unsubscribe();
|
1368
|
-
}
|
1369
|
-
};
|
1370
|
-
|
1371
|
-
centrifugeProto.publish = function (channel, data, callback) {
|
1372
|
-
var subscription = this.getSubscription(channel);
|
1373
|
-
if (subscription === null) {
|
1374
|
-
this._debug("subscription not found for channel " + channel);
|
1375
|
-
return null;
|
1376
|
-
}
|
1377
|
-
subscription.publish(data, callback);
|
1378
|
-
return subscription;
|
1379
|
-
};
|
1380
|
-
|
1381
|
-
centrifugeProto.presence = function (channel, callback) {
|
1382
|
-
var subscription = this.getSubscription(channel);
|
1383
|
-
if (subscription === null) {
|
1384
|
-
this._debug("subscription not found for channel " + channel);
|
1385
|
-
return null;
|
1386
|
-
}
|
1387
|
-
subscription.presence(callback);
|
1388
|
-
return subscription;
|
1389
|
-
};
|
1390
|
-
|
1391
|
-
centrifugeProto.history = function (channel, callback) {
|
1392
|
-
var subscription = this.getSubscription(channel);
|
1393
|
-
if (subscription === null) {
|
1394
|
-
this._debug("subscription not found for channel " + channel);
|
1395
|
-
return null;
|
1396
|
-
}
|
1397
|
-
subscription.history(callback);
|
1398
|
-
return subscription;
|
1399
|
-
};
|
1400
|
-
|
1401
|
-
centrifugeProto.refresh = function () {
|
1402
|
-
// ask web app for connection parameters - project ID, user ID,
|
1403
|
-
// timestamp, info and token
|
1404
|
-
var self = this;
|
1405
|
-
this._debug('refresh');
|
1406
|
-
AJAX.request(this._config.refreshEndpoint, "post", {
|
1407
|
-
"headers": this._config.refreshHeaders,
|
1408
|
-
"data": {}
|
1409
|
-
}).done(function(data) {
|
1410
|
-
self._config.user = data.user;
|
1411
|
-
self._config.project = data.project;
|
1412
|
-
self._config.timestamp = data.timestamp;
|
1413
|
-
self._config.info = data.info;
|
1414
|
-
self._config.token = data.token;
|
1415
|
-
if (self._reconnect && self.isDisconnected()) {
|
1416
|
-
self.connect();
|
1417
|
-
} else {
|
1418
|
-
var centrifugeMessage = {
|
1419
|
-
"method": "refresh",
|
1420
|
-
"params": {
|
1421
|
-
'user': self._config.user,
|
1422
|
-
'project': self._config.project,
|
1423
|
-
'timestamp': self._config.timestamp,
|
1424
|
-
'info': self._config.info,
|
1425
|
-
'token': self._config.token
|
1426
|
-
}
|
1427
|
-
};
|
1428
|
-
self.send(centrifugeMessage);
|
1429
|
-
}
|
1430
|
-
}).fail(function(xhr){
|
1431
|
-
// 403 or 500 - does not matter - if connection check activated then Centrifuge
|
1432
|
-
// will disconnect client eventually
|
1433
|
-
self._debug(xhr);
|
1434
|
-
self._debug("error getting connect parameters");
|
1435
|
-
if (self._refreshTimeout) {
|
1436
|
-
window.clearTimeout(self._refreshTimeout);
|
1437
|
-
}
|
1438
|
-
self._refreshTimeout = window.setTimeout(function(){
|
1439
|
-
self.refresh.call(self);
|
1440
|
-
}, 3000);
|
1441
|
-
});
|
1442
|
-
};
|
1443
|
-
|
1444
|
-
function Subscription(centrifuge, channel) {
|
1445
|
-
/**
|
1446
|
-
* The constructor for a centrifuge object, identified by an optional name.
|
1447
|
-
* The default name is the string 'default'.
|
1448
|
-
* @param name the optional name of this centrifuge object
|
1449
|
-
*/
|
1450
|
-
this._centrifuge = centrifuge;
|
1451
|
-
this.channel = channel;
|
1452
|
-
}
|
1453
|
-
|
1454
|
-
extend(Subscription, EventEmitter);
|
1455
|
-
|
1456
|
-
var subscriptionProto = Subscription.prototype;
|
1457
|
-
|
1458
|
-
subscriptionProto.getChannel = function () {
|
1459
|
-
return this.channel;
|
1460
|
-
};
|
1461
|
-
|
1462
|
-
subscriptionProto.getCentrifuge = function () {
|
1463
|
-
return this._centrifuge;
|
1464
|
-
};
|
1465
|
-
|
1466
|
-
subscriptionProto.subscribe = function (callback) {
|
1467
|
-
/*
|
1468
|
-
If channel name does not start with privateChannelPrefix - then we
|
1469
|
-
can just send subscription message to Centrifuge. If channel name
|
1470
|
-
starts with privateChannelPrefix - then this is a private channel
|
1471
|
-
and we should ask web application backend for permission first.
|
1472
|
-
*/
|
1473
|
-
var centrifugeMessage = {
|
1474
|
-
"method": "subscribe",
|
1475
|
-
"params": {
|
1476
|
-
"channel": this.channel
|
1477
|
-
}
|
1478
|
-
};
|
1479
|
-
|
1480
|
-
if (startsWith(this.channel, this._centrifuge._config.privateChannelPrefix)) {
|
1481
|
-
// private channel
|
1482
|
-
if (this._centrifuge._isAuthBatching) {
|
1483
|
-
this._centrifuge._authChannels[this.channel] = true;
|
1484
|
-
} else {
|
1485
|
-
this._centrifuge.startAuthBatching();
|
1486
|
-
this.subscribe(callback);
|
1487
|
-
this._centrifuge.stopAuthBatching();
|
1488
|
-
}
|
1489
|
-
} else {
|
1490
|
-
this._centrifuge.send(centrifugeMessage);
|
1491
|
-
}
|
1492
|
-
|
1493
|
-
if (callback) {
|
1494
|
-
this.on('message', callback);
|
1495
|
-
}
|
1496
|
-
};
|
1497
|
-
|
1498
|
-
subscriptionProto.unsubscribe = function () {
|
1499
|
-
this._centrifuge._removeSubscription(this.channel);
|
1500
|
-
var centrifugeMessage = {
|
1501
|
-
"method": "unsubscribe",
|
1502
|
-
"params": {
|
1503
|
-
"channel": this.channel
|
1504
|
-
}
|
1505
|
-
};
|
1506
|
-
this._centrifuge.send(centrifugeMessage);
|
1507
|
-
};
|
1508
|
-
|
1509
|
-
subscriptionProto.publish = function (data, callback) {
|
1510
|
-
var centrifugeMessage = {
|
1511
|
-
"method": "publish",
|
1512
|
-
"params": {
|
1513
|
-
"channel": this.channel,
|
1514
|
-
"data": data
|
1515
|
-
}
|
1516
|
-
};
|
1517
|
-
if (callback) {
|
1518
|
-
this.on('publish:success', callback);
|
1519
|
-
}
|
1520
|
-
this._centrifuge.send(centrifugeMessage);
|
1521
|
-
};
|
1522
|
-
|
1523
|
-
subscriptionProto.presence = function (callback) {
|
1524
|
-
var centrifugeMessage = {
|
1525
|
-
"method": "presence",
|
1526
|
-
"params": {
|
1527
|
-
"channel": this.channel
|
1528
|
-
}
|
1529
|
-
};
|
1530
|
-
if (callback) {
|
1531
|
-
this.on('presence', callback);
|
1532
|
-
}
|
1533
|
-
this._centrifuge.send(centrifugeMessage);
|
1534
|
-
};
|
1535
|
-
|
1536
|
-
subscriptionProto.history = function (callback) {
|
1537
|
-
var centrifugeMessage = {
|
1538
|
-
"method": "history",
|
1539
|
-
"params": {
|
1540
|
-
"channel": this.channel
|
1541
|
-
}
|
1542
|
-
};
|
1543
|
-
if (callback) {
|
1544
|
-
this.on('history', callback);
|
1545
|
-
}
|
1546
|
-
this._centrifuge.send(centrifugeMessage);
|
1547
|
-
};
|
1548
|
-
|
1549
|
-
// Expose the class either via AMD, CommonJS or the global object
|
1550
|
-
if (typeof define === 'function' && define.amd) {
|
1551
|
-
define(function () {
|
1552
|
-
return Centrifuge;
|
1553
|
-
});
|
1554
|
-
} else if (typeof module === 'object' && module.exports) {
|
1555
|
-
//noinspection JSUnresolvedVariable
|
1556
|
-
module.exports = Centrifuge;
|
1557
|
-
} else {
|
1558
|
-
//noinspection JSUnusedGlobalSymbols
|
1559
|
-
this.Centrifuge = Centrifuge;
|
1560
|
-
}
|
1561
|
-
|
1562
|
-
}.call(this));
|
1
|
+
(function(){"use strict";function e(e,t){return e.prototype=Object.create(t.prototype),e.prototype.constructor=e,t.prototype}function t(){}function n(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function i(e){return function(){return this[e].apply(this,arguments)}}function s(e,t,n){for(var i=t||{},o=2;o<arguments.length;++o){var c=arguments[o];if(void 0!==c&&null!==c)for(var u in c){var h=r(c,u),a=r(i,u);if(h!==t&&void 0!==h)if(e&&"object"==typeof h&&null!==h)if(h instanceof Array)i[u]=s(e,a instanceof Array?a:[],h);else{var f="object"!=typeof a||a instanceof Array?{}:a;i[u]=s(e,f,h)}else i[u]=h}}return i}function r(e,t){try{return e[t]}catch(n){return void 0}}function o(e,t){return-1!==e.indexOf(t,e.length-t.length)}function c(e,t){return 0===e.lastIndexOf(t,0)}function u(e){return"/"==e.substring(e.length-1)&&(e=e.substring(0,e.length-1)),e}function h(e){return void 0===e||null===e?!1:"string"==typeof e||e instanceof String}function a(e){return void 0===e||null===e?!1:"function"==typeof e}function f(e,t){if(window.console){var n=window.console[e];a(n)&&n.apply(window.console,t)}}function l(e){this._sockjs=!1,this._sockjsVersion=null,this._status="disconnected",this._reconnect=!0,this._transport=null,this._messageId=0,this._clientId=null,this._subscriptions={},this._messages=[],this._isBatching=!1,this._isAuthBatching=!1,this._authChannels={},this._refreshTimeout=null,this._config={retry:3e3,info:"",debug:!1,insecure:!1,server:null,protocols_whitelist:["websocket","xdr-streaming","xhr-streaming","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"],transports:["websocket","xdr-streaming","xhr-streaming","eventsource","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"],privateChannelPrefix:"$",refreshEndpoint:"/centrifuge/refresh",authEndpoint:"/centrifuge/auth",authHeaders:{},refreshHeaders:{}},e&&this.configure(e)}function g(e,t){this._centrifuge=e,this.channel=t}Object.create||(Object.create=function(){function e(){}return function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}()),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){if(null==this)throw new TypeError;var t,n,i=Object(this),s=i.length>>>0;if(0===s)return-1;if(t=0,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&t!=1/0&&t!=-(1/0)&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=s)return-1;for(n=t>=0?t:Math.max(s-Math.abs(t),0);s>n;n++)if(n in i&&i[n]===e)return n;return-1});var p=t.prototype;p.getListeners=function(e){var t,n,i=this._getEvents();if("object"==typeof e){t={};for(n in i)i.hasOwnProperty(n)&&e.test(n)&&(t[n]=i[n])}else t=i[e]||(i[e]=[]);return t},p.flattenListeners=function(e){var t,n=[];for(t=0;t<e.length;t+=1)n.push(e[t].listener);return n},p.getListenersAsObject=function(e){var t,n=this.getListeners(e);return n instanceof Array&&(t={},t[e]=n),t||n},p.addListener=function(e,t){var i,s=this.getListenersAsObject(e),r="object"==typeof t;for(i in s)s.hasOwnProperty(i)&&-1===n(s[i],t)&&s[i].push(r?t:{listener:t,once:!1});return this},p.on=i("addListener"),p.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},p.once=i("addOnceListener"),p.defineEvent=function(e){return this.getListeners(e),this},p.defineEvents=function(e){for(var t=0;t<e.length;t+=1)this.defineEvent(e[t]);return this},p.removeListener=function(e,t){var i,s,r=this.getListenersAsObject(e);for(s in r)r.hasOwnProperty(s)&&(i=n(r[s],t),-1!==i&&r[s].splice(i,1));return this},p.off=i("removeListener"),p.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},p.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},p.manipulateListeners=function(e,t,n){var i,s,r=e?this.removeListener:this.addListener,o=e?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(i=n.length;i--;)r.call(this,t,n[i]);else for(i in t)t.hasOwnProperty(i)&&(s=t[i])&&("function"==typeof s?r.call(this,i,s):o.call(this,i,s));return this},p.removeEvent=function(e){var t,n=typeof e,i=this._getEvents();if("string"===n)delete i[e];else if("object"===n)for(t in i)i.hasOwnProperty(t)&&e.test(t)&&delete i[t];else delete this._events;return this},p.emitEvent=function(e,t){var n,i,s,r,o=this.getListenersAsObject(e);for(s in o)if(o.hasOwnProperty(s))for(i=o[s].length;i--;)n=o[s][i],n.once===!0&&this.removeListener(e,n.listener),r=n.listener.apply(this,t||[]),r===this._getOnceReturnValue()&&this.removeListener(e,n.listener);return this},p.trigger=i("emitEvent"),p.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},p.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},p._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},p._getEvents=function(){return this._events||(this._events={})};var d={request:function(e,t,n){n.data=n.data||{};var i=function(e,t){var n,i=[];for(var s in e)if("object"==typeof e[s])for(var r in e[s])i.push(s+"="+encodeURIComponent(e[s][r]));else i.push(s+"="+encodeURIComponent(e[s]));return n=i.join("&"),""!=n?t?t.indexOf("?")<0?"?"+n:"&"+n:n:""},s={host:{},setHeaders:function(e){for(var t in e)this.xhr&&this.xhr.setRequestHeader(t,e[t])},process:function(n){var s=this;return this.xhr=null,window.ActiveXObject?this.xhr=new ActiveXObject("Microsoft.XMLHTTP"):window.XMLHttpRequest&&(this.xhr=new XMLHttpRequest),this.xhr&&(this.xhr.onreadystatechange=function(){if(4==s.xhr.readyState&&200==s.xhr.status){var e=s.xhr.responseText;if("undefined"==typeof JSON)throw"JSON undefined";e=JSON.parse(e),s.doneCallback&&s.doneCallback.apply(s.host,[e,s.xhr])}else 4==s.xhr.readyState&&s.failCallback&&s.failCallback.apply(s.host,[s.xhr]);s.alwaysCallback&&s.alwaysCallback.apply(s.host,[s.xhr])}),"get"==t?this.xhr.open("GET",e+i(n.data,e),!0):(this.xhr.open(t,e,!0),this.setHeaders({"X-Requested-With":"XMLHttpRequest","Content-type":"application/x-www-form-urlencoded"})),n.headers&&"object"==typeof n.headers&&this.setHeaders(n.headers),setTimeout(function(){"get"==t?s.xhr.send():s.xhr.send(i(n.data))},20),this},done:function(e){return this.doneCallback=e,this},fail:function(e){return this.failCallback=e,this},always:function(e){return this.alwaysCallback=e,this}};return s.process(n)}};e(l,t);var _=l.prototype;_._log=function(){f("info",arguments)},_._debug=function(){this._config.debug===!0&&f("debug",arguments)},_._configure=function(e){if(this._debug("Configuring centrifuge object with",e),e||(e={}),this._config=s(!1,this._config,e),!this._config.url)throw"Missing required configuration parameter 'url' specifying server URL";if(!this._config.project)throw"Missing required configuration parameter 'project' specifying project key in server configuration";if(!this._config.user&&""!==this._config.user){if(!this._config.insecure)throw"Missing required configuration parameter 'user' specifying user's unique ID in your application";this._debug("user not found but this is OK for insecure mode - anonymous access will be used"),this._config.user=""}if(!this._config.timestamp){if(!this._config.insecure)throw"Missing required configuration parameter 'timestamp'";this._debug("token not found but this is OK for insecure mode")}if(!this._config.token){if(!this._config.insecure)throw"Missing required configuration parameter 'token' specifying the sign of authorization request";this._debug("timestamp not found but this is OK for insecure mode")}if(this._config.url=u(this._config.url),o(this._config.url,"connection")){if(this._debug("client will connect to SockJS endpoint"),"undefined"==typeof SockJS)throw"include SockJS client library before Centrifuge javascript client library or use raw Websocket connection endpoint";this._sockjs=!0,this._sockjsVersion=SockJS.version}else o(this._config.url,"connection/websocket")?(this._debug("client will connect to raw Websocket endpoint"),this._config.url=this._config.url.replace("http://","ws://"),this._config.url=this._config.url.replace("https://","wss://")):(this._debug("client will detect connection endpoint itself"),"undefined"==typeof SockJS?(this._debug("no SockJS found, client will connect to raw Websocket endpoint"),this._config.url+="/connection/websocket",this._config.url=this._config.url.replace("http://","ws://"),this._config.url=this._config.url.replace("https://","wss://")):(this._debug("SockJS found, client will connect to SockJS endpoint"),this._config.url+="/connection",this._sockjs=!0,this._sockjsVersion=SockJS.version))},_._setStatus=function(e){this._status!==e&&(this._debug("Status",this._status,"->",e),this._status=e)},_._isDisconnected=function(){return this._isConnected()===!1},_._isConnected=function(){return"connected"===this._status},_._isConnecting=function(){return"connecting"===this._status},_._nextMessageId=function(){return++this._messageId},_._clearSubscriptions=function(){this._subscriptions={}},_._send=function(e){for(var t=0;t<e.length;++t){var n=e[t];n.uid=""+this._nextMessageId(),this._clientId&&(n.clientId=this._clientId),this._debug("Send",n),this._transport.send(JSON.stringify(n))}},_._connect=function(e){if(!this.isConnected()){this._clientId=null,this._reconnect=!0,this._clearSubscriptions(),this._setStatus("connecting");var t=this;if(e&&this.on("connect",e),this._sockjs===!0){var n={};c(this._sockjsVersion,"1.")?n.transports=this._config.transports:(this._log("SockJS <= 0.3.4 is deprecated, use SockJS >= 1.0.0 instead"),n.protocols_whitelist=this._config.protocols_whitelist),null!==this._config.server&&(n.server=this._config.server),this._transport=new SockJS(this._config.url,null,n)}else this._transport=new WebSocket(this._config.url);this._setStatus("connecting"),this._transport.onopen=function(){var e={method:"connect",params:{user:t._config.user,project:t._config.project,info:t._config.info}};t._config.insecure||(e.params.timestamp=t._config.timestamp,e.params.token=t._config.token),t.send(e)},this._transport.onerror=function(e){t._debug(e)},this._transport.onclose=function(){t._setStatus("disconnected"),t.trigger("disconnect"),t._reconnect===!0&&window.setTimeout(function(){t._reconnect===!0&&t._connect.call(t)},t._config.retry)},this._transport.onmessage=function(e){var n;n=JSON.parse(e.data),t._debug("Received",n),t._receive(n)}}},_._disconnect=function(){this._clientId=null,this._setStatus("disconnected"),this._subscriptions={},this._reconnect=!1,this._transport.close()},_._getSubscription=function(e){var t;return t=this._subscriptions[e],t?t:null},_._removeSubscription=function(e){try{delete this._subscriptions[e]}catch(t){this._debug("nothing to delete for channel ",e)}try{delete this._authChannels[e]}catch(t){this._debug("nothing to delete from authChannels for channel ",e)}},_._connectResponse=function(e){if(!this.isConnected())if(null===e.error){if(!e.body)return;var t=e.body.expired;if(t)return void this.refresh();if(this._clientId=e.body.client,this._setStatus("connected"),this.trigger("connect",[e]),this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),null!==e.body.ttl){var n=this;this._refreshTimeout=window.setTimeout(function(){n.refresh.call(n)},1e3*e.body.ttl)}}else this.trigger("error",[e]),this.trigger("connect:error",[e])},_._disconnectResponse=function(e){null===e.error?this.disconnect():(this.trigger("error",[e]),this.trigger("disconnect:error",[e.error]))},_._subscribeResponse=function(e){null!==e.error&&this.trigger("error",[e]);var t=e.body;if(null!==t){var n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("subscribe:success",[t]),i.trigger("ready",[t])):(i.trigger("subscribe:error",[e.error]),i.trigger("error",[e])))}},_._unsubscribeResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&null===e.error&&(i.trigger("unsubscribe",[t]),this._centrifuge._removeSubscription(n))},_._publishResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?i.trigger("publish:success",[t]):(i.trigger("publish:error",[e.error]),this.trigger("error",[e])))},_._presenceResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("presence",[t]),i.trigger("presence:success",[t])):(i.trigger("presence:error",[e.error]),this.trigger("error",[e])))},_._historyResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("history",[t]),i.trigger("history:success",[t])):(i.trigger("history:error",[e.error]),this.trigger("error",[e])))},_._joinResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("join",[t])},_._leaveResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("leave",[t])},_._messageResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);null!==i&&i.trigger("message",[t])},_._refreshResponse=function(e){if(this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),null!==e.body.ttl){var t=this;t._refreshTimeout=window.setTimeout(function(){t.refresh.call(t)},1e3*e.body.ttl)}},_._dispatchMessage=function(e){if(void 0!==e&&null!==e){var t=e.method;if(t)switch(t){case"connect":this._connectResponse(e);break;case"disconnect":this._disconnectResponse(e);break;case"subscribe":this._subscribeResponse(e);break;case"unsubscribe":this._unsubscribeResponse(e);break;case"publish":this._publishResponse(e);break;case"presence":this._presenceResponse(e);break;case"history":this._historyResponse(e);break;case"join":this._joinResponse(e);break;case"leave":this._leaveResponse(e);break;case"ping":break;case"refresh":this._refreshResponse(e);break;case"message":this._messageResponse(e)}}},_._receive=function(e){if(Object.prototype.toString.call(e)===Object.prototype.toString.call([])){for(var t in e)if(e.hasOwnProperty(t)){var n=e[t];this._dispatchMessage(n)}}else Object.prototype.toString.call(e)===Object.prototype.toString.call({})&&this._dispatchMessage(e)},_._flush=function(){var e=this._messages.slice(0);this._messages=[],this._send(e)},_._ping=function(){var e={method:"ping",params:{}};this.send(e)},_.getClientId=function(){return this._clientId},_.isConnected=_._isConnected,_.isConnecting=_._isConnecting,_.isDisconnected=_._isDisconnected,_.configure=function(e){this._configure.call(this,e)},_.connect=_._connect,_.disconnect=_._disconnect,_.getSubscription=_._getSubscription,_.ping=_._ping,_.send=function(e){this._isBatching===!0?this._messages.push(e):this._send([e])},_.startBatching=function(){this._isBatching=!0},_.stopBatching=function(e){e=e||!1,this._isBatching=!1,e===!0&&this.flush()},_.flush=function(){this._flush()},_.startAuthBatching=function(){this._isAuthBatching=!0},_.stopAuthBatching=function(e){this._isAuthBatching=!1;var t=this._authChannels;this._authChannels={};var n=[];for(var i in t){var s=this.getSubscription(i);s&&n.push(i)}if(0==n.length)return void(e&&e());var r={client:this.getClientId(),"channels[]":n},o=this;d.request(this._config.authEndpoint,"post",{headers:this._config.authHeaders,data:r}).done(function(e){for(var t in n){var i=n[t],s=e[i];if(s)if(s.status&&200!==s.status)o._subscribeResponse({error:s.status,body:{channel:i}});else{var r={method:"subscribe",params:{channel:i,client:o.getClientId(),info:s.info,sign:s.sign}};o.send(r)}else o._subscribeResponse({error:404,body:{channel:i}})}}).fail(function(){o._debug("authorization request failed");for(var e in n){var t=n[e];o._subscribeResponse({error:"authorization request failed",body:{channel:t}})}return!1}).always(function(){e&&e()})},_.subscribe=function(e,t){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(this.isDisconnected())throw"Can not subscribe in disconnected state";var n=this.getSubscription(e);if(null!==n)return n;var i=new g(this,e);return this._subscriptions[e]=i,i.subscribe(t),i},_.unsubscribe=function(e){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(!this.isDisconnected()){var t=this.getSubscription(e);null!==t&&t.unsubscribe()}},_.publish=function(e,t,n){var i=this.getSubscription(e);return null===i?(this._debug("subscription not found for channel "+e),null):(i.publish(t,n),i)},_.presence=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.presence(t),n)},_.history=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.history(t),n)},_.refresh=function(){var e=this;this._debug("refresh"),d.request(this._config.refreshEndpoint,"post",{headers:this._config.refreshHeaders,data:{}}).done(function(t){if(e._config.user=t.user,e._config.project=t.project,e._config.timestamp=t.timestamp,e._config.info=t.info,e._config.token=t.token,e._reconnect&&e.isDisconnected())e.connect();else{var n={method:"refresh",params:{user:e._config.user,project:e._config.project,timestamp:e._config.timestamp,info:e._config.info,token:e._config.token}};e.send(n)}}).fail(function(t){e._debug(t),e._debug("error getting connect parameters"),e._refreshTimeout&&window.clearTimeout(e._refreshTimeout),e._refreshTimeout=window.setTimeout(function(){e.refresh.call(e)},3e3)})},e(g,t);var b=g.prototype;b.getChannel=function(){return this.channel},b.getCentrifuge=function(){return this._centrifuge},b.subscribe=function(e){var t={method:"subscribe",params:{channel:this.channel}};c(this.channel,this._centrifuge._config.privateChannelPrefix)?this._centrifuge._isAuthBatching?this._centrifuge._authChannels[this.channel]=!0:(this._centrifuge.startAuthBatching(),this.subscribe(e),this._centrifuge.stopAuthBatching()):this._centrifuge.send(t),e&&this.on("message",e)},b.unsubscribe=function(){this._centrifuge._removeSubscription(this.channel);var e={method:"unsubscribe",params:{channel:this.channel}};this._centrifuge.send(e)},b.publish=function(e,t){var n={method:"publish",params:{channel:this.channel,data:e}};t&&this.on("publish:success",t),this._centrifuge.send(n)},b.presence=function(e){var t={method:"presence",params:{channel:this.channel}};e&&this.on("presence",e),this._centrifuge.send(t)},b.history=function(e){var t={method:"history",params:{channel:this.channel}};e&&this.on("history",e),this._centrifuge.send(t)},"function"==typeof define&&define.amd?define(function(){return l}):"object"==typeof module&&module.exports?module.exports=l:this.Centrifuge=l}).call(this);
|