faye 1.0.4 → 1.1.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/CHANGELOG.md +34 -2
- package/browser/faye-browser-min.js +1 -1
- package/browser/faye-browser-min.js.map +1 -1
- package/browser/faye-browser.js +580 -347
- package/node/faye-node.js +652 -351
- package/package.json +8 -7
package/browser/faye-browser.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var Faye = {
|
|
5
|
-
VERSION: '1.
|
|
5
|
+
VERSION: '1.1.3',
|
|
6
6
|
|
|
7
7
|
BAYEUX_VERSION: '1.0',
|
|
8
8
|
ID_LENGTH: 160,
|
|
@@ -26,7 +26,17 @@ var Faye = {
|
|
|
26
26
|
|
|
27
27
|
random: function(bitlength) {
|
|
28
28
|
bitlength = bitlength || this.ID_LENGTH;
|
|
29
|
-
|
|
29
|
+
var maxLength = Math.ceil(bitlength * Math.log(2) / Math.log(36));
|
|
30
|
+
var string = csprng(bitlength, 36);
|
|
31
|
+
while (string.length < maxLength) string = '0' + string;
|
|
32
|
+
return string;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
validateOptions: function(options, validKeys) {
|
|
36
|
+
for (var key in options) {
|
|
37
|
+
if (this.indexOf(validKeys, key) < 0)
|
|
38
|
+
throw new Error('Unrecognized option: ' + key);
|
|
39
|
+
}
|
|
30
40
|
},
|
|
31
41
|
|
|
32
42
|
clientIdFromMessages: function(messages) {
|
|
@@ -357,9 +367,8 @@ Faye.Namespace = Faye.Class({
|
|
|
357
367
|
(function() {
|
|
358
368
|
'use strict';
|
|
359
369
|
|
|
360
|
-
var timeout = setTimeout;
|
|
370
|
+
var timeout = setTimeout, defer;
|
|
361
371
|
|
|
362
|
-
var defer;
|
|
363
372
|
if (typeof setImmediate === 'function')
|
|
364
373
|
defer = function(fn) { setImmediate(fn) };
|
|
365
374
|
else if (typeof process === 'object' && process.nextTick)
|
|
@@ -372,12 +381,12 @@ var PENDING = 0,
|
|
|
372
381
|
REJECTED = 2;
|
|
373
382
|
|
|
374
383
|
var RETURN = function(x) { return x },
|
|
375
|
-
THROW = function(x) { throw
|
|
384
|
+
THROW = function(x) { throw x };
|
|
376
385
|
|
|
377
386
|
var Promise = function(task) {
|
|
378
|
-
this._state
|
|
379
|
-
this.
|
|
380
|
-
this.
|
|
387
|
+
this._state = PENDING;
|
|
388
|
+
this._onFulfilled = [];
|
|
389
|
+
this._onRejected = [];
|
|
381
390
|
|
|
382
391
|
if (typeof task !== 'function') return;
|
|
383
392
|
var self = this;
|
|
@@ -386,34 +395,30 @@ var Promise = function(task) {
|
|
|
386
395
|
function(reason) { reject(self, reason) });
|
|
387
396
|
};
|
|
388
397
|
|
|
389
|
-
Promise.prototype.then = function(
|
|
390
|
-
var next =
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
next.reject = reject;
|
|
395
|
-
|
|
396
|
-
registerCallback(self, callback, next);
|
|
397
|
-
registerErrback(self, errback, next);
|
|
398
|
-
});
|
|
399
|
-
return next.promise;
|
|
398
|
+
Promise.prototype.then = function(onFulfilled, onRejected) {
|
|
399
|
+
var next = new Promise();
|
|
400
|
+
registerOnFulfilled(this, onFulfilled, next);
|
|
401
|
+
registerOnRejected(this, onRejected, next);
|
|
402
|
+
return next;
|
|
400
403
|
};
|
|
401
404
|
|
|
402
|
-
var
|
|
403
|
-
if (typeof
|
|
404
|
-
var handler = function(value) { invoke(
|
|
405
|
+
var registerOnFulfilled = function(promise, onFulfilled, next) {
|
|
406
|
+
if (typeof onFulfilled !== 'function') onFulfilled = RETURN;
|
|
407
|
+
var handler = function(value) { invoke(onFulfilled, value, next) };
|
|
408
|
+
|
|
405
409
|
if (promise._state === PENDING) {
|
|
406
|
-
promise.
|
|
410
|
+
promise._onFulfilled.push(handler);
|
|
407
411
|
} else if (promise._state === FULFILLED) {
|
|
408
412
|
handler(promise._value);
|
|
409
413
|
}
|
|
410
414
|
};
|
|
411
415
|
|
|
412
|
-
var
|
|
413
|
-
if (typeof
|
|
414
|
-
var handler = function(reason) { invoke(
|
|
416
|
+
var registerOnRejected = function(promise, onRejected, next) {
|
|
417
|
+
if (typeof onRejected !== 'function') onRejected = THROW;
|
|
418
|
+
var handler = function(reason) { invoke(onRejected, reason, next) };
|
|
419
|
+
|
|
415
420
|
if (promise._state === PENDING) {
|
|
416
|
-
promise.
|
|
421
|
+
promise._onRejected.push(handler);
|
|
417
422
|
} else if (promise._state === REJECTED) {
|
|
418
423
|
handler(promise._reason);
|
|
419
424
|
}
|
|
@@ -424,55 +429,80 @@ var invoke = function(fn, value, next) {
|
|
|
424
429
|
};
|
|
425
430
|
|
|
426
431
|
var _invoke = function(fn, value, next) {
|
|
427
|
-
var
|
|
432
|
+
var outcome;
|
|
428
433
|
|
|
429
434
|
try {
|
|
430
435
|
outcome = fn(value);
|
|
431
|
-
|
|
432
|
-
|
|
436
|
+
} catch (error) {
|
|
437
|
+
return reject(next, error);
|
|
438
|
+
}
|
|
433
439
|
|
|
434
|
-
|
|
435
|
-
|
|
440
|
+
if (outcome === next) {
|
|
441
|
+
reject(next, new TypeError('Recursive promise chain detected'));
|
|
442
|
+
} else {
|
|
443
|
+
fulfill(next, outcome);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
var fulfill = Promise.fulfill = Promise.resolve = function(promise, value) {
|
|
448
|
+
var called = false, type, then;
|
|
436
449
|
|
|
437
|
-
|
|
450
|
+
try {
|
|
451
|
+
type = typeof value;
|
|
452
|
+
then = value !== null && (type === 'function' || type === 'object') && value.then;
|
|
453
|
+
|
|
454
|
+
if (typeof then !== 'function') return _fulfill(promise, value);
|
|
438
455
|
|
|
439
|
-
then.call(
|
|
440
|
-
if (called) return;
|
|
441
|
-
|
|
442
|
-
_invoke(RETURN, v, next);
|
|
456
|
+
then.call(value, function(v) {
|
|
457
|
+
if (!(called ^ (called = true))) return;
|
|
458
|
+
fulfill(promise, v);
|
|
443
459
|
}, function(r) {
|
|
444
|
-
if (called) return;
|
|
445
|
-
|
|
446
|
-
next.reject(r);
|
|
460
|
+
if (!(called ^ (called = true))) return;
|
|
461
|
+
reject(promise, r);
|
|
447
462
|
});
|
|
448
|
-
|
|
449
463
|
} catch (error) {
|
|
450
|
-
if (called) return;
|
|
451
|
-
|
|
452
|
-
next.reject(error);
|
|
464
|
+
if (!(called ^ (called = true))) return;
|
|
465
|
+
reject(promise, error);
|
|
453
466
|
}
|
|
454
467
|
};
|
|
455
468
|
|
|
456
|
-
var
|
|
469
|
+
var _fulfill = function(promise, value) {
|
|
457
470
|
if (promise._state !== PENDING) return;
|
|
458
471
|
|
|
459
|
-
promise._state
|
|
460
|
-
promise._value
|
|
461
|
-
promise.
|
|
472
|
+
promise._state = FULFILLED;
|
|
473
|
+
promise._value = value;
|
|
474
|
+
promise._onRejected = [];
|
|
462
475
|
|
|
463
|
-
var
|
|
464
|
-
while (
|
|
476
|
+
var onFulfilled = promise._onFulfilled, fn;
|
|
477
|
+
while (fn = onFulfilled.shift()) fn(value);
|
|
465
478
|
};
|
|
466
479
|
|
|
467
480
|
var reject = Promise.reject = function(promise, reason) {
|
|
468
481
|
if (promise._state !== PENDING) return;
|
|
469
482
|
|
|
470
|
-
promise._state
|
|
471
|
-
promise._reason
|
|
472
|
-
promise.
|
|
483
|
+
promise._state = REJECTED;
|
|
484
|
+
promise._reason = reason;
|
|
485
|
+
promise._onFulfilled = [];
|
|
486
|
+
|
|
487
|
+
var onRejected = promise._onRejected, fn;
|
|
488
|
+
while (fn = onRejected.shift()) fn(reason);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
Promise.all = function(promises) {
|
|
492
|
+
return new Promise(function(fulfill, reject) {
|
|
493
|
+
var list = [],
|
|
494
|
+
n = promises.length,
|
|
495
|
+
i;
|
|
496
|
+
|
|
497
|
+
if (n === 0) return fulfill(list);
|
|
473
498
|
|
|
474
|
-
|
|
475
|
-
|
|
499
|
+
for (i = 0; i < n; i++) (function(promise, i) {
|
|
500
|
+
Promise.fulfilled(promise).then(function(value) {
|
|
501
|
+
list[i] = value;
|
|
502
|
+
if (--n === 0) fulfill(list);
|
|
503
|
+
}, reject);
|
|
504
|
+
})(promises[i], i);
|
|
505
|
+
});
|
|
476
506
|
};
|
|
477
507
|
|
|
478
508
|
Promise.defer = defer;
|
|
@@ -744,7 +774,7 @@ Faye.Deferrable = {
|
|
|
744
774
|
setDeferredStatus: function(status, value) {
|
|
745
775
|
if (this._timer) Faye.ENV.clearTimeout(this._timer);
|
|
746
776
|
|
|
747
|
-
|
|
777
|
+
this.then();
|
|
748
778
|
|
|
749
779
|
if (status === 'succeeded')
|
|
750
780
|
this._fulfill(value);
|
|
@@ -801,7 +831,7 @@ Faye.Timeouts = {
|
|
|
801
831
|
this._timeouts = this._timeouts || {};
|
|
802
832
|
var timeout = this._timeouts[name];
|
|
803
833
|
if (!timeout) return;
|
|
804
|
-
clearTimeout(timeout);
|
|
834
|
+
Faye.ENV.clearTimeout(timeout);
|
|
805
835
|
delete this._timeouts[name];
|
|
806
836
|
},
|
|
807
837
|
|
|
@@ -823,13 +853,13 @@ Faye.Logging = {
|
|
|
823
853
|
writeLog: function(messageArgs, level) {
|
|
824
854
|
if (!Faye.logger) return;
|
|
825
855
|
|
|
826
|
-
var
|
|
827
|
-
banner
|
|
828
|
-
klass
|
|
856
|
+
var args = Array.prototype.slice.apply(messageArgs),
|
|
857
|
+
banner = '[Faye',
|
|
858
|
+
klass = this.className,
|
|
829
859
|
|
|
830
|
-
message =
|
|
860
|
+
message = args.shift().replace(/\?/g, function() {
|
|
831
861
|
try {
|
|
832
|
-
return Faye.toJSON(
|
|
862
|
+
return Faye.toJSON(args.shift());
|
|
833
863
|
} catch (e) {
|
|
834
864
|
return '[Object]';
|
|
835
865
|
}
|
|
@@ -852,11 +882,11 @@ Faye.Logging = {
|
|
|
852
882
|
|
|
853
883
|
(function() {
|
|
854
884
|
for (var key in Faye.Logging.LOG_LEVELS)
|
|
855
|
-
(function(level
|
|
885
|
+
(function(level) {
|
|
856
886
|
Faye.Logging[level] = function() {
|
|
857
887
|
this.writeLog(arguments, level);
|
|
858
888
|
};
|
|
859
|
-
})(key
|
|
889
|
+
})(key);
|
|
860
890
|
})();
|
|
861
891
|
|
|
862
892
|
Faye.Grammar = {
|
|
@@ -999,12 +1029,11 @@ Faye.extend(Faye.Channel, {
|
|
|
999
1029
|
},
|
|
1000
1030
|
|
|
1001
1031
|
subscribe: function(names, callback, context) {
|
|
1002
|
-
if (!callback) return;
|
|
1003
1032
|
var name;
|
|
1004
1033
|
for (var i = 0, n = names.length; i < n; i++) {
|
|
1005
1034
|
name = names[i];
|
|
1006
1035
|
var channel = this._channels[name] = this._channels[name] || new Faye.Channel(name);
|
|
1007
|
-
channel.bind('message', callback, context);
|
|
1036
|
+
if (callback) channel.bind('message', callback, context);
|
|
1008
1037
|
}
|
|
1009
1038
|
},
|
|
1010
1039
|
|
|
@@ -1032,17 +1061,6 @@ Faye.extend(Faye.Channel, {
|
|
|
1032
1061
|
})
|
|
1033
1062
|
});
|
|
1034
1063
|
|
|
1035
|
-
Faye.Envelope = Faye.Class({
|
|
1036
|
-
initialize: function(message, timeout) {
|
|
1037
|
-
this.id = message.id;
|
|
1038
|
-
this.message = message;
|
|
1039
|
-
|
|
1040
|
-
if (timeout !== undefined) this.timeout(timeout / 1000, false);
|
|
1041
|
-
}
|
|
1042
|
-
});
|
|
1043
|
-
|
|
1044
|
-
Faye.extend(Faye.Envelope.prototype, Faye.Deferrable);
|
|
1045
|
-
|
|
1046
1064
|
Faye.Publication = Faye.Class(Faye.Deferrable);
|
|
1047
1065
|
|
|
1048
1066
|
Faye.Subscription = Faye.Class({
|
|
@@ -1068,65 +1086,61 @@ Faye.Subscription = Faye.Class({
|
|
|
1068
1086
|
Faye.extend(Faye.Subscription.prototype, Faye.Deferrable);
|
|
1069
1087
|
|
|
1070
1088
|
Faye.Client = Faye.Class({
|
|
1071
|
-
UNCONNECTED:
|
|
1072
|
-
CONNECTING:
|
|
1073
|
-
CONNECTED:
|
|
1074
|
-
DISCONNECTED:
|
|
1089
|
+
UNCONNECTED: 1,
|
|
1090
|
+
CONNECTING: 2,
|
|
1091
|
+
CONNECTED: 3,
|
|
1092
|
+
DISCONNECTED: 4,
|
|
1075
1093
|
|
|
1076
|
-
HANDSHAKE:
|
|
1077
|
-
RETRY:
|
|
1078
|
-
NONE:
|
|
1094
|
+
HANDSHAKE: 'handshake',
|
|
1095
|
+
RETRY: 'retry',
|
|
1096
|
+
NONE: 'none',
|
|
1079
1097
|
|
|
1080
|
-
CONNECTION_TIMEOUT:
|
|
1081
|
-
DEFAULT_RETRY: 5,
|
|
1082
|
-
MAX_REQUEST_SIZE: 2048,
|
|
1098
|
+
CONNECTION_TIMEOUT: 60,
|
|
1083
1099
|
|
|
1084
|
-
DEFAULT_ENDPOINT:
|
|
1085
|
-
INTERVAL:
|
|
1100
|
+
DEFAULT_ENDPOINT: '/bayeux',
|
|
1101
|
+
INTERVAL: 0,
|
|
1086
1102
|
|
|
1087
1103
|
initialize: function(endpoint, options) {
|
|
1088
1104
|
this.info('New client created for ?', endpoint);
|
|
1105
|
+
options = options || {};
|
|
1089
1106
|
|
|
1090
|
-
|
|
1091
|
-
this.endpoint = Faye.URI.parse(endpoint || this.DEFAULT_ENDPOINT);
|
|
1092
|
-
this.endpoints = this._options.endpoints || {};
|
|
1093
|
-
this.transports = {};
|
|
1094
|
-
this.cookies = Faye.CookieJar && new Faye.CookieJar();
|
|
1095
|
-
this.headers = {};
|
|
1096
|
-
this.ca = this._options.ca;
|
|
1097
|
-
this._disabled = [];
|
|
1098
|
-
this._retry = this._options.retry || this.DEFAULT_RETRY;
|
|
1099
|
-
|
|
1100
|
-
for (var key in this.endpoints)
|
|
1101
|
-
this.endpoints[key] = Faye.URI.parse(this.endpoints[key]);
|
|
1107
|
+
Faye.validateOptions(options, ['interval', 'timeout', 'endpoints', 'proxy', 'retry', 'scheduler', 'websocketExtensions', 'tls', 'ca']);
|
|
1102
1108
|
|
|
1103
|
-
this.
|
|
1109
|
+
this._endpoint = endpoint || this.DEFAULT_ENDPOINT;
|
|
1110
|
+
this._channels = new Faye.Channel.Set();
|
|
1111
|
+
this._dispatcher = new Faye.Dispatcher(this, this._endpoint, options);
|
|
1104
1112
|
|
|
1105
|
-
this._state = this.UNCONNECTED;
|
|
1106
|
-
this._channels = new Faye.Channel.Set();
|
|
1107
1113
|
this._messageId = 0;
|
|
1114
|
+
this._state = this.UNCONNECTED;
|
|
1108
1115
|
|
|
1109
1116
|
this._responseCallbacks = {};
|
|
1110
1117
|
|
|
1111
1118
|
this._advice = {
|
|
1112
1119
|
reconnect: this.RETRY,
|
|
1113
|
-
interval: 1000 * (
|
|
1114
|
-
timeout: 1000 * (
|
|
1120
|
+
interval: 1000 * (options.interval || this.INTERVAL),
|
|
1121
|
+
timeout: 1000 * (options.timeout || this.CONNECTION_TIMEOUT)
|
|
1115
1122
|
};
|
|
1123
|
+
this._dispatcher.timeout = this._advice.timeout / 1000;
|
|
1124
|
+
|
|
1125
|
+
this._dispatcher.bind('message', this._receiveMessage, this);
|
|
1116
1126
|
|
|
1117
1127
|
if (Faye.Event && Faye.ENV.onbeforeunload !== undefined)
|
|
1118
1128
|
Faye.Event.on(Faye.ENV, 'beforeunload', function() {
|
|
1119
|
-
if (Faye.indexOf(this._disabled, 'autodisconnect') < 0)
|
|
1129
|
+
if (Faye.indexOf(this._dispatcher._disabled, 'autodisconnect') < 0)
|
|
1120
1130
|
this.disconnect();
|
|
1121
1131
|
}, this);
|
|
1122
1132
|
},
|
|
1123
1133
|
|
|
1134
|
+
addWebsocketExtension: function(extension) {
|
|
1135
|
+
return this._dispatcher.addWebsocketExtension(extension);
|
|
1136
|
+
},
|
|
1137
|
+
|
|
1124
1138
|
disable: function(feature) {
|
|
1125
|
-
this.
|
|
1139
|
+
return this._dispatcher.disable(feature);
|
|
1126
1140
|
},
|
|
1127
1141
|
|
|
1128
1142
|
setHeader: function(name, value) {
|
|
1129
|
-
this.
|
|
1143
|
+
return this._dispatcher.setHeader(name, value);
|
|
1130
1144
|
},
|
|
1131
1145
|
|
|
1132
1146
|
// Request
|
|
@@ -1155,30 +1169,30 @@ Faye.Client = Faye.Class({
|
|
|
1155
1169
|
this._state = this.CONNECTING;
|
|
1156
1170
|
var self = this;
|
|
1157
1171
|
|
|
1158
|
-
this.info('Initiating handshake with ?', Faye.URI.stringify(this.
|
|
1159
|
-
this.
|
|
1172
|
+
this.info('Initiating handshake with ?', Faye.URI.stringify(this._endpoint));
|
|
1173
|
+
this._dispatcher.selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
|
|
1160
1174
|
|
|
1161
|
-
this.
|
|
1175
|
+
this._sendMessage({
|
|
1162
1176
|
channel: Faye.Channel.HANDSHAKE,
|
|
1163
1177
|
version: Faye.BAYEUX_VERSION,
|
|
1164
|
-
supportedConnectionTypes:
|
|
1178
|
+
supportedConnectionTypes: this._dispatcher.getConnectionTypes()
|
|
1165
1179
|
|
|
1166
|
-
}, function(response) {
|
|
1180
|
+
}, {}, function(response) {
|
|
1167
1181
|
|
|
1168
1182
|
if (response.successful) {
|
|
1169
|
-
this._state
|
|
1170
|
-
this.
|
|
1183
|
+
this._state = this.CONNECTED;
|
|
1184
|
+
this._dispatcher.clientId = response.clientId;
|
|
1171
1185
|
|
|
1172
|
-
this.
|
|
1186
|
+
this._dispatcher.selectTransport(response.supportedConnectionTypes);
|
|
1173
1187
|
|
|
1174
|
-
this.info('Handshake successful: ?', this.
|
|
1188
|
+
this.info('Handshake successful: ?', this._dispatcher.clientId);
|
|
1175
1189
|
|
|
1176
1190
|
this.subscribe(this._channels.getKeys(), true);
|
|
1177
1191
|
if (callback) Faye.Promise.defer(function() { callback.call(context) });
|
|
1178
1192
|
|
|
1179
1193
|
} else {
|
|
1180
1194
|
this.info('Handshake unsuccessful');
|
|
1181
|
-
Faye.ENV.setTimeout(function() { self.handshake(callback, context) }, this.
|
|
1195
|
+
Faye.ENV.setTimeout(function() { self.handshake(callback, context) }, this._dispatcher.retry * 1000);
|
|
1182
1196
|
this._state = this.UNCONNECTED;
|
|
1183
1197
|
}
|
|
1184
1198
|
}, this);
|
|
@@ -1203,21 +1217,21 @@ Faye.Client = Faye.Class({
|
|
|
1203
1217
|
this.callback(callback, context);
|
|
1204
1218
|
if (this._state !== this.CONNECTED) return;
|
|
1205
1219
|
|
|
1206
|
-
this.info('Calling deferred actions for ?', this.
|
|
1220
|
+
this.info('Calling deferred actions for ?', this._dispatcher.clientId);
|
|
1207
1221
|
this.setDeferredStatus('succeeded');
|
|
1208
1222
|
this.setDeferredStatus('unknown');
|
|
1209
1223
|
|
|
1210
1224
|
if (this._connectRequest) return;
|
|
1211
1225
|
this._connectRequest = true;
|
|
1212
1226
|
|
|
1213
|
-
this.info('Initiating connection for ?', this.
|
|
1227
|
+
this.info('Initiating connection for ?', this._dispatcher.clientId);
|
|
1214
1228
|
|
|
1215
|
-
this.
|
|
1229
|
+
this._sendMessage({
|
|
1216
1230
|
channel: Faye.Channel.CONNECT,
|
|
1217
|
-
clientId: this.
|
|
1218
|
-
connectionType: this.
|
|
1231
|
+
clientId: this._dispatcher.clientId,
|
|
1232
|
+
connectionType: this._dispatcher.connectionType
|
|
1219
1233
|
|
|
1220
|
-
}, this._cycleConnection, this);
|
|
1234
|
+
}, {}, this._cycleConnection, this);
|
|
1221
1235
|
},
|
|
1222
1236
|
|
|
1223
1237
|
// Request Response
|
|
@@ -1231,20 +1245,26 @@ Faye.Client = Faye.Class({
|
|
|
1231
1245
|
if (this._state !== this.CONNECTED) return;
|
|
1232
1246
|
this._state = this.DISCONNECTED;
|
|
1233
1247
|
|
|
1234
|
-
this.info('Disconnecting ?', this.
|
|
1248
|
+
this.info('Disconnecting ?', this._dispatcher.clientId);
|
|
1249
|
+
var promise = new Faye.Publication();
|
|
1235
1250
|
|
|
1236
|
-
this.
|
|
1251
|
+
this._sendMessage({
|
|
1237
1252
|
channel: Faye.Channel.DISCONNECT,
|
|
1238
|
-
clientId: this.
|
|
1253
|
+
clientId: this._dispatcher.clientId
|
|
1239
1254
|
|
|
1240
|
-
}, function(response) {
|
|
1241
|
-
if (
|
|
1242
|
-
|
|
1243
|
-
|
|
1255
|
+
}, {}, function(response) {
|
|
1256
|
+
if (response.successful) {
|
|
1257
|
+
this._dispatcher.close();
|
|
1258
|
+
promise.setDeferredStatus('succeeded');
|
|
1259
|
+
} else {
|
|
1260
|
+
promise.setDeferredStatus('failed', Faye.Error.parse(response.error));
|
|
1261
|
+
}
|
|
1244
1262
|
}, this);
|
|
1245
1263
|
|
|
1246
|
-
this.info('Clearing channel listeners for ?', this.
|
|
1264
|
+
this.info('Clearing channel listeners for ?', this._dispatcher.clientId);
|
|
1247
1265
|
this._channels = new Faye.Channel.Set();
|
|
1266
|
+
|
|
1267
|
+
return promise;
|
|
1248
1268
|
},
|
|
1249
1269
|
|
|
1250
1270
|
// Request Response
|
|
@@ -1274,22 +1294,22 @@ Faye.Client = Faye.Class({
|
|
|
1274
1294
|
}
|
|
1275
1295
|
|
|
1276
1296
|
this.connect(function() {
|
|
1277
|
-
this.info('Client ? attempting to subscribe to ?', this.
|
|
1297
|
+
this.info('Client ? attempting to subscribe to ?', this._dispatcher.clientId, channel);
|
|
1278
1298
|
if (!force) this._channels.subscribe([channel], callback, context);
|
|
1279
1299
|
|
|
1280
|
-
this.
|
|
1300
|
+
this._sendMessage({
|
|
1281
1301
|
channel: Faye.Channel.SUBSCRIBE,
|
|
1282
|
-
clientId: this.
|
|
1302
|
+
clientId: this._dispatcher.clientId,
|
|
1283
1303
|
subscription: channel
|
|
1284
1304
|
|
|
1285
|
-
}, function(response) {
|
|
1305
|
+
}, {}, function(response) {
|
|
1286
1306
|
if (!response.successful) {
|
|
1287
1307
|
subscription.setDeferredStatus('failed', Faye.Error.parse(response.error));
|
|
1288
1308
|
return this._channels.unsubscribe(channel, callback, context);
|
|
1289
1309
|
}
|
|
1290
1310
|
|
|
1291
1311
|
var channels = [].concat(response.subscription);
|
|
1292
|
-
this.info('Subscription acknowledged for ? to ?', this.
|
|
1312
|
+
this.info('Subscription acknowledged for ? to ?', this._dispatcher.clientId, channels);
|
|
1293
1313
|
subscription.setDeferredStatus('succeeded');
|
|
1294
1314
|
}, this);
|
|
1295
1315
|
}, this);
|
|
@@ -1317,18 +1337,18 @@ Faye.Client = Faye.Class({
|
|
|
1317
1337
|
if (!dead) return;
|
|
1318
1338
|
|
|
1319
1339
|
this.connect(function() {
|
|
1320
|
-
this.info('Client ? attempting to unsubscribe from ?', this.
|
|
1340
|
+
this.info('Client ? attempting to unsubscribe from ?', this._dispatcher.clientId, channel);
|
|
1321
1341
|
|
|
1322
|
-
this.
|
|
1342
|
+
this._sendMessage({
|
|
1323
1343
|
channel: Faye.Channel.UNSUBSCRIBE,
|
|
1324
|
-
clientId: this.
|
|
1344
|
+
clientId: this._dispatcher.clientId,
|
|
1325
1345
|
subscription: channel
|
|
1326
1346
|
|
|
1327
|
-
}, function(response) {
|
|
1347
|
+
}, {}, function(response) {
|
|
1328
1348
|
if (!response.successful) return;
|
|
1329
1349
|
|
|
1330
1350
|
var channels = [].concat(response.subscription);
|
|
1331
|
-
this.info('Unsubscription acknowledged for ? from ?', this.
|
|
1351
|
+
this.info('Unsubscription acknowledged for ? from ?', this._dispatcher.clientId, channels);
|
|
1332
1352
|
}, this);
|
|
1333
1353
|
}, this);
|
|
1334
1354
|
},
|
|
@@ -1339,18 +1359,19 @@ Faye.Client = Faye.Class({
|
|
|
1339
1359
|
// MAY include: * clientId MAY include: * id
|
|
1340
1360
|
// * id * error
|
|
1341
1361
|
// * ext * ext
|
|
1342
|
-
publish: function(channel, data) {
|
|
1362
|
+
publish: function(channel, data, options) {
|
|
1363
|
+
Faye.validateOptions(options || {}, ['attempts', 'deadline']);
|
|
1343
1364
|
var publication = new Faye.Publication();
|
|
1344
1365
|
|
|
1345
1366
|
this.connect(function() {
|
|
1346
|
-
this.info('Client ? queueing published message to ?: ?', this.
|
|
1367
|
+
this.info('Client ? queueing published message to ?: ?', this._dispatcher.clientId, channel, data);
|
|
1347
1368
|
|
|
1348
|
-
this.
|
|
1369
|
+
this._sendMessage({
|
|
1349
1370
|
channel: channel,
|
|
1350
1371
|
data: data,
|
|
1351
|
-
clientId: this.
|
|
1372
|
+
clientId: this._dispatcher.clientId
|
|
1352
1373
|
|
|
1353
|
-
}, function(response) {
|
|
1374
|
+
}, options, function(response) {
|
|
1354
1375
|
if (response.successful)
|
|
1355
1376
|
publication.setDeferredStatus('succeeded');
|
|
1356
1377
|
else
|
|
@@ -1361,8 +1382,28 @@ Faye.Client = Faye.Class({
|
|
|
1361
1382
|
return publication;
|
|
1362
1383
|
},
|
|
1363
1384
|
|
|
1364
|
-
|
|
1365
|
-
|
|
1385
|
+
_sendMessage: function(message, options, callback, context) {
|
|
1386
|
+
message.id = this._generateMessageId();
|
|
1387
|
+
|
|
1388
|
+
var timeout = this._advice.timeout
|
|
1389
|
+
? 1.2 * this._advice.timeout / 1000
|
|
1390
|
+
: 1.2 * this._dispatcher.retry;
|
|
1391
|
+
|
|
1392
|
+
this.pipeThroughExtensions('outgoing', message, null, function(message) {
|
|
1393
|
+
if (!message) return;
|
|
1394
|
+
if (callback) this._responseCallbacks[message.id] = [callback, context];
|
|
1395
|
+
this._dispatcher.sendMessage(message, timeout, options || {});
|
|
1396
|
+
}, this);
|
|
1397
|
+
},
|
|
1398
|
+
|
|
1399
|
+
_generateMessageId: function() {
|
|
1400
|
+
this._messageId += 1;
|
|
1401
|
+
if (this._messageId >= Math.pow(2,32)) this._messageId = 0;
|
|
1402
|
+
return this._messageId.toString(36);
|
|
1403
|
+
},
|
|
1404
|
+
|
|
1405
|
+
_receiveMessage: function(message) {
|
|
1406
|
+
var id = message.id, callback;
|
|
1366
1407
|
|
|
1367
1408
|
if (message.successful !== undefined) {
|
|
1368
1409
|
callback = this._responseCallbacks[id];
|
|
@@ -1371,39 +1412,112 @@ Faye.Client = Faye.Class({
|
|
|
1371
1412
|
|
|
1372
1413
|
this.pipeThroughExtensions('incoming', message, null, function(message) {
|
|
1373
1414
|
if (!message) return;
|
|
1374
|
-
|
|
1375
1415
|
if (message.advice) this._handleAdvice(message.advice);
|
|
1376
1416
|
this._deliverMessage(message);
|
|
1377
|
-
|
|
1378
1417
|
if (callback) callback[0].call(callback[1], message);
|
|
1379
1418
|
}, this);
|
|
1419
|
+
},
|
|
1420
|
+
|
|
1421
|
+
_handleAdvice: function(advice) {
|
|
1422
|
+
Faye.extend(this._advice, advice);
|
|
1423
|
+
this._dispatcher.timeout = this._advice.timeout / 1000;
|
|
1424
|
+
|
|
1425
|
+
if (this._advice.reconnect === this.HANDSHAKE && this._state !== this.DISCONNECTED) {
|
|
1426
|
+
this._state = this.UNCONNECTED;
|
|
1427
|
+
this._dispatcher.clientId = null;
|
|
1428
|
+
this._cycleConnection();
|
|
1429
|
+
}
|
|
1430
|
+
},
|
|
1380
1431
|
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
this.
|
|
1432
|
+
_deliverMessage: function(message) {
|
|
1433
|
+
if (!message.channel || message.data === undefined) return;
|
|
1434
|
+
this.info('Client ? calling listeners for ? with ?', this._dispatcher.clientId, message.channel, message.data);
|
|
1435
|
+
this._channels.distributeMessage(message);
|
|
1384
1436
|
},
|
|
1385
1437
|
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1438
|
+
_cycleConnection: function() {
|
|
1439
|
+
if (this._connectRequest) {
|
|
1440
|
+
this._connectRequest = null;
|
|
1441
|
+
this.info('Closed connection for ?', this._dispatcher.clientId);
|
|
1442
|
+
}
|
|
1443
|
+
var self = this;
|
|
1444
|
+
Faye.ENV.setTimeout(function() { self.connect() }, this._advice.interval);
|
|
1445
|
+
}
|
|
1446
|
+
});
|
|
1390
1447
|
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1448
|
+
Faye.extend(Faye.Client.prototype, Faye.Deferrable);
|
|
1449
|
+
Faye.extend(Faye.Client.prototype, Faye.Publisher);
|
|
1450
|
+
Faye.extend(Faye.Client.prototype, Faye.Logging);
|
|
1451
|
+
Faye.extend(Faye.Client.prototype, Faye.Extensible);
|
|
1394
1452
|
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1453
|
+
Faye.Dispatcher = Faye.Class({
|
|
1454
|
+
MAX_REQUEST_SIZE: 2048,
|
|
1455
|
+
DEFAULT_RETRY: 5,
|
|
1456
|
+
|
|
1457
|
+
UP: 1,
|
|
1458
|
+
DOWN: 2,
|
|
1459
|
+
|
|
1460
|
+
initialize: function(client, endpoint, options) {
|
|
1461
|
+
this._client = client;
|
|
1462
|
+
this.endpoint = Faye.URI.parse(endpoint);
|
|
1463
|
+
this._alternates = options.endpoints || {};
|
|
1464
|
+
|
|
1465
|
+
this.cookies = Faye.Cookies && new Faye.Cookies.CookieJar();
|
|
1466
|
+
this._disabled = [];
|
|
1467
|
+
this._envelopes = {};
|
|
1468
|
+
this.headers = {};
|
|
1469
|
+
this.retry = options.retry || this.DEFAULT_RETRY;
|
|
1470
|
+
this._scheduler = options.scheduler || Faye.Scheduler;
|
|
1471
|
+
this._state = 0;
|
|
1472
|
+
this.transports = {};
|
|
1473
|
+
this.wsExtensions = [];
|
|
1474
|
+
|
|
1475
|
+
this.proxy = options.proxy || {};
|
|
1476
|
+
if (typeof this._proxy === 'string') this._proxy = {origin: this._proxy};
|
|
1477
|
+
|
|
1478
|
+
var exts = options.websocketExtensions;
|
|
1479
|
+
if (exts) {
|
|
1480
|
+
exts = [].concat(exts);
|
|
1481
|
+
for (var i = 0, n = exts.length; i < n; i++)
|
|
1482
|
+
this.addWebsocketExtension(exts[i]);
|
|
1399
1483
|
}
|
|
1400
1484
|
|
|
1401
|
-
|
|
1402
|
-
this.
|
|
1403
|
-
|
|
1485
|
+
this.tls = options.tls || {};
|
|
1486
|
+
this.tls.ca = this.tls.ca || options.ca;
|
|
1487
|
+
|
|
1488
|
+
for (var type in this._alternates)
|
|
1489
|
+
this._alternates[type] = Faye.URI.parse(this._alternates[type]);
|
|
1490
|
+
|
|
1491
|
+
this.maxRequestSize = this.MAX_REQUEST_SIZE;
|
|
1492
|
+
},
|
|
1493
|
+
|
|
1494
|
+
endpointFor: function(connectionType) {
|
|
1495
|
+
return this._alternates[connectionType] || this.endpoint;
|
|
1496
|
+
},
|
|
1497
|
+
|
|
1498
|
+
addWebsocketExtension: function(extension) {
|
|
1499
|
+
this.wsExtensions.push(extension);
|
|
1500
|
+
},
|
|
1501
|
+
|
|
1502
|
+
disable: function(feature) {
|
|
1503
|
+
this._disabled.push(feature);
|
|
1404
1504
|
},
|
|
1405
1505
|
|
|
1406
|
-
|
|
1506
|
+
setHeader: function(name, value) {
|
|
1507
|
+
this.headers[name] = value;
|
|
1508
|
+
},
|
|
1509
|
+
|
|
1510
|
+
close: function() {
|
|
1511
|
+
var transport = this._transport;
|
|
1512
|
+
delete this._transport;
|
|
1513
|
+
if (transport) transport.close();
|
|
1514
|
+
},
|
|
1515
|
+
|
|
1516
|
+
getConnectionTypes: function() {
|
|
1517
|
+
return Faye.Transport.getConnectionTypes();
|
|
1518
|
+
},
|
|
1519
|
+
|
|
1520
|
+
selectTransport: function(transportTypes) {
|
|
1407
1521
|
Faye.Transport.get(this, transportTypes, this._disabled, function(transport) {
|
|
1408
1522
|
this.debug('Selected ? transport for ?', transport.connectionType, Faye.URI.stringify(transport.endpoint));
|
|
1409
1523
|
|
|
@@ -1411,186 +1525,275 @@ Faye.Client = Faye.Class({
|
|
|
1411
1525
|
if (this._transport) this._transport.close();
|
|
1412
1526
|
|
|
1413
1527
|
this._transport = transport;
|
|
1528
|
+
this.connectionType = transport.connectionType;
|
|
1414
1529
|
}, this);
|
|
1415
1530
|
},
|
|
1416
1531
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
message.id = message.id || this._generateMessageId();
|
|
1532
|
+
sendMessage: function(message, timeout, options) {
|
|
1533
|
+
options = options || {};
|
|
1420
1534
|
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1535
|
+
var id = message.id,
|
|
1536
|
+
attempts = options.attempts,
|
|
1537
|
+
deadline = options.deadline && new Date().getTime() + (options.deadline * 1000),
|
|
1538
|
+
envelope = this._envelopes[id],
|
|
1539
|
+
scheduler;
|
|
1540
|
+
|
|
1541
|
+
if (!envelope) {
|
|
1542
|
+
scheduler = new this._scheduler(message, {timeout: timeout, interval: this.retry, attempts: attempts, deadline: deadline});
|
|
1543
|
+
envelope = this._envelopes[id] = {message: message, scheduler: scheduler};
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
this._sendEnvelope(envelope);
|
|
1426
1547
|
},
|
|
1427
1548
|
|
|
1428
|
-
|
|
1549
|
+
_sendEnvelope: function(envelope) {
|
|
1429
1550
|
if (!this._transport) return;
|
|
1551
|
+
if (envelope.request || envelope.timer) return;
|
|
1430
1552
|
|
|
1431
|
-
var
|
|
1432
|
-
|
|
1553
|
+
var message = envelope.message,
|
|
1554
|
+
scheduler = envelope.scheduler,
|
|
1555
|
+
self = this;
|
|
1433
1556
|
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1557
|
+
if (!scheduler.isDeliverable()) {
|
|
1558
|
+
scheduler.abort();
|
|
1559
|
+
delete this._envelopes[message.id];
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1437
1562
|
|
|
1438
|
-
|
|
1439
|
-
|
|
1563
|
+
envelope.timer = Faye.ENV.setTimeout(function() {
|
|
1564
|
+
self.handleError(message);
|
|
1565
|
+
}, scheduler.getTimeout() * 1000);
|
|
1440
1566
|
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
if (this._messageId >= Math.pow(2,32)) this._messageId = 0;
|
|
1444
|
-
return this._messageId.toString(36);
|
|
1567
|
+
scheduler.send();
|
|
1568
|
+
envelope.request = this._transport.sendMessage(message);
|
|
1445
1569
|
},
|
|
1446
1570
|
|
|
1447
|
-
|
|
1448
|
-
|
|
1571
|
+
handleResponse: function(reply) {
|
|
1572
|
+
var envelope = this._envelopes[reply.id];
|
|
1449
1573
|
|
|
1450
|
-
if (
|
|
1451
|
-
|
|
1452
|
-
this.
|
|
1453
|
-
|
|
1574
|
+
if (reply.successful !== undefined && envelope) {
|
|
1575
|
+
envelope.scheduler.succeed();
|
|
1576
|
+
delete this._envelopes[reply.id];
|
|
1577
|
+
Faye.ENV.clearTimeout(envelope.timer);
|
|
1454
1578
|
}
|
|
1455
|
-
},
|
|
1456
1579
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
this.
|
|
1460
|
-
this.
|
|
1580
|
+
this.trigger('message', reply);
|
|
1581
|
+
|
|
1582
|
+
if (this._state === this.UP) return;
|
|
1583
|
+
this._state = this.UP;
|
|
1584
|
+
this._client.trigger('transport:up');
|
|
1461
1585
|
},
|
|
1462
1586
|
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1587
|
+
handleError: function(message, immediate) {
|
|
1588
|
+
var envelope = this._envelopes[message.id],
|
|
1589
|
+
request = envelope && envelope.request,
|
|
1590
|
+
self = this;
|
|
1591
|
+
|
|
1592
|
+
if (!request) return;
|
|
1593
|
+
|
|
1594
|
+
request.then(function(req) {
|
|
1595
|
+
if (req && req.abort) req.abort();
|
|
1596
|
+
});
|
|
1597
|
+
|
|
1598
|
+
var scheduler = envelope.scheduler;
|
|
1599
|
+
scheduler.fail();
|
|
1600
|
+
|
|
1601
|
+
Faye.ENV.clearTimeout(envelope.timer);
|
|
1602
|
+
envelope.request = envelope.timer = null;
|
|
1603
|
+
|
|
1604
|
+
if (immediate) {
|
|
1605
|
+
this._sendEnvelope(envelope);
|
|
1606
|
+
} else {
|
|
1607
|
+
envelope.timer = Faye.ENV.setTimeout(function() {
|
|
1608
|
+
envelope.timer = null;
|
|
1609
|
+
self._sendEnvelope(envelope);
|
|
1610
|
+
}, scheduler.getInterval() * 1000);
|
|
1467
1611
|
}
|
|
1468
|
-
|
|
1469
|
-
|
|
1612
|
+
|
|
1613
|
+
if (this._state === this.DOWN) return;
|
|
1614
|
+
this._state = this.DOWN;
|
|
1615
|
+
this._client.trigger('transport:down');
|
|
1470
1616
|
}
|
|
1471
1617
|
});
|
|
1472
1618
|
|
|
1473
|
-
Faye.extend(Faye.
|
|
1474
|
-
Faye.extend(Faye.
|
|
1475
|
-
|
|
1476
|
-
Faye.
|
|
1619
|
+
Faye.extend(Faye.Dispatcher.prototype, Faye.Publisher);
|
|
1620
|
+
Faye.extend(Faye.Dispatcher.prototype, Faye.Logging);
|
|
1621
|
+
|
|
1622
|
+
Faye.Scheduler = function(message, options) {
|
|
1623
|
+
this.message = message;
|
|
1624
|
+
this.options = options;
|
|
1625
|
+
this.attempts = 0;
|
|
1626
|
+
};
|
|
1627
|
+
|
|
1628
|
+
Faye.extend(Faye.Scheduler.prototype, {
|
|
1629
|
+
getTimeout: function() {
|
|
1630
|
+
return this.options.timeout;
|
|
1631
|
+
},
|
|
1632
|
+
|
|
1633
|
+
getInterval: function() {
|
|
1634
|
+
return this.options.interval;
|
|
1635
|
+
},
|
|
1636
|
+
|
|
1637
|
+
isDeliverable: function() {
|
|
1638
|
+
var attempts = this.options.attempts,
|
|
1639
|
+
made = this.attempts,
|
|
1640
|
+
deadline = this.options.deadline,
|
|
1641
|
+
now = new Date().getTime();
|
|
1642
|
+
|
|
1643
|
+
if (attempts !== undefined && made >= attempts)
|
|
1644
|
+
return false;
|
|
1645
|
+
|
|
1646
|
+
if (deadline !== undefined && now > deadline)
|
|
1647
|
+
return false;
|
|
1648
|
+
|
|
1649
|
+
return true;
|
|
1650
|
+
},
|
|
1651
|
+
|
|
1652
|
+
send: function() {
|
|
1653
|
+
this.attempts += 1;
|
|
1654
|
+
},
|
|
1655
|
+
|
|
1656
|
+
succeed: function() {},
|
|
1657
|
+
|
|
1658
|
+
fail: function() {},
|
|
1659
|
+
|
|
1660
|
+
abort: function() {}
|
|
1661
|
+
});
|
|
1477
1662
|
|
|
1478
1663
|
Faye.Transport = Faye.extend(Faye.Class({
|
|
1479
|
-
|
|
1664
|
+
DEFAULT_PORTS: {'http:': 80, 'https:': 443, 'ws:': 80, 'wss:': 443},
|
|
1665
|
+
SECURE_PROTOCOLS: ['https:', 'wss:'],
|
|
1666
|
+
MAX_DELAY: 0,
|
|
1667
|
+
|
|
1480
1668
|
batching: true,
|
|
1481
1669
|
|
|
1482
|
-
initialize: function(
|
|
1483
|
-
this.
|
|
1484
|
-
this.endpoint
|
|
1485
|
-
this._outbox
|
|
1670
|
+
initialize: function(dispatcher, endpoint) {
|
|
1671
|
+
this._dispatcher = dispatcher;
|
|
1672
|
+
this.endpoint = endpoint;
|
|
1673
|
+
this._outbox = [];
|
|
1674
|
+
this._proxy = Faye.extend({}, this._dispatcher.proxy);
|
|
1675
|
+
|
|
1676
|
+
if (!this._proxy.origin && Faye.NodeAdapter) {
|
|
1677
|
+
this._proxy.origin = Faye.indexOf(this.SECURE_PROTOCOLS, this.endpoint.protocol) >= 0
|
|
1678
|
+
? (process.env.HTTPS_PROXY || process.env.https_proxy)
|
|
1679
|
+
: (process.env.HTTP_PROXY || process.env.http_proxy);
|
|
1680
|
+
}
|
|
1486
1681
|
},
|
|
1487
1682
|
|
|
1488
1683
|
close: function() {},
|
|
1489
1684
|
|
|
1490
|
-
encode: function(
|
|
1685
|
+
encode: function(messages) {
|
|
1491
1686
|
return '';
|
|
1492
1687
|
},
|
|
1493
1688
|
|
|
1494
|
-
|
|
1495
|
-
var message = envelope.message;
|
|
1496
|
-
|
|
1689
|
+
sendMessage: function(message) {
|
|
1497
1690
|
this.debug('Client ? sending message to ?: ?',
|
|
1498
|
-
this.
|
|
1691
|
+
this._dispatcher.clientId, Faye.URI.stringify(this.endpoint), message);
|
|
1499
1692
|
|
|
1500
|
-
if (!this.batching) return this.request([
|
|
1693
|
+
if (!this.batching) return Faye.Promise.fulfilled(this.request([message]));
|
|
1501
1694
|
|
|
1502
|
-
this._outbox.push(
|
|
1695
|
+
this._outbox.push(message);
|
|
1696
|
+
this._promise = this._promise || new Faye.Promise();
|
|
1697
|
+
this._flushLargeBatch();
|
|
1503
1698
|
|
|
1504
|
-
if (message.channel === Faye.Channel.HANDSHAKE)
|
|
1505
|
-
|
|
1699
|
+
if (message.channel === Faye.Channel.HANDSHAKE) {
|
|
1700
|
+
this.addTimeout('publish', 0.01, this._flush, this);
|
|
1701
|
+
return this._promise;
|
|
1702
|
+
}
|
|
1506
1703
|
|
|
1507
1704
|
if (message.channel === Faye.Channel.CONNECT)
|
|
1508
1705
|
this._connectMessage = message;
|
|
1509
1706
|
|
|
1510
|
-
this.
|
|
1511
|
-
|
|
1707
|
+
this.addTimeout('publish', this.MAX_DELAY, this._flush, this);
|
|
1708
|
+
return this._promise;
|
|
1512
1709
|
},
|
|
1513
1710
|
|
|
1514
|
-
|
|
1711
|
+
_flush: function() {
|
|
1515
1712
|
this.removeTimeout('publish');
|
|
1516
1713
|
|
|
1517
1714
|
if (this._outbox.length > 1 && this._connectMessage)
|
|
1518
1715
|
this._connectMessage.advice = {timeout: 0};
|
|
1519
1716
|
|
|
1520
|
-
this.request(this._outbox);
|
|
1717
|
+
Faye.Promise.fulfill(this._promise, this.request(this._outbox));
|
|
1718
|
+
delete this._promise;
|
|
1521
1719
|
|
|
1522
1720
|
this._connectMessage = null;
|
|
1523
1721
|
this._outbox = [];
|
|
1524
1722
|
},
|
|
1525
1723
|
|
|
1526
|
-
|
|
1724
|
+
_flushLargeBatch: function() {
|
|
1527
1725
|
var string = this.encode(this._outbox);
|
|
1528
|
-
if (string.length < this.
|
|
1726
|
+
if (string.length < this._dispatcher.maxRequestSize) return;
|
|
1529
1727
|
var last = this._outbox.pop();
|
|
1530
|
-
this.
|
|
1728
|
+
this._flush();
|
|
1531
1729
|
if (last) this._outbox.push(last);
|
|
1532
1730
|
},
|
|
1533
1731
|
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
responses = [].concat(responses);
|
|
1732
|
+
_receive: function(replies) {
|
|
1733
|
+
if (!replies) return;
|
|
1734
|
+
replies = [].concat(replies);
|
|
1539
1735
|
|
|
1540
|
-
this.debug('Client ? received from ?: ?',
|
|
1541
|
-
this.
|
|
1736
|
+
this.debug('Client ? received from ? via ?: ?',
|
|
1737
|
+
this._dispatcher.clientId, Faye.URI.stringify(this.endpoint), this.connectionType, replies);
|
|
1542
1738
|
|
|
1543
|
-
for (var i = 0, n =
|
|
1544
|
-
this.
|
|
1739
|
+
for (var i = 0, n = replies.length; i < n; i++)
|
|
1740
|
+
this._dispatcher.handleResponse(replies[i]);
|
|
1545
1741
|
},
|
|
1546
1742
|
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1743
|
+
_handleError: function(messages, immediate) {
|
|
1744
|
+
messages = [].concat(messages);
|
|
1745
|
+
|
|
1746
|
+
this.debug('Client ? failed to send to ? via ?: ?',
|
|
1747
|
+
this._dispatcher.clientId, Faye.URI.stringify(this.endpoint), this.connectionType, messages);
|
|
1748
|
+
|
|
1749
|
+
for (var i = 0, n = messages.length; i < n; i++)
|
|
1750
|
+
this._dispatcher.handleError(messages[i]);
|
|
1550
1751
|
},
|
|
1551
1752
|
|
|
1552
1753
|
_getCookies: function() {
|
|
1553
|
-
var cookies = this.
|
|
1754
|
+
var cookies = this._dispatcher.cookies,
|
|
1755
|
+
url = Faye.URI.stringify(this.endpoint);
|
|
1756
|
+
|
|
1554
1757
|
if (!cookies) return '';
|
|
1555
1758
|
|
|
1556
|
-
return cookies.
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
secure: this.endpoint.protocol === 'https:'
|
|
1560
|
-
}).toValueString();
|
|
1759
|
+
return Faye.map(cookies.getCookiesSync(url), function(cookie) {
|
|
1760
|
+
return cookie.cookieString();
|
|
1761
|
+
}).join('; ');
|
|
1561
1762
|
},
|
|
1562
1763
|
|
|
1563
1764
|
_storeCookies: function(setCookie) {
|
|
1564
|
-
|
|
1765
|
+
var cookies = this._dispatcher.cookies,
|
|
1766
|
+
url = Faye.URI.stringify(this.endpoint),
|
|
1767
|
+
cookie;
|
|
1768
|
+
|
|
1769
|
+
if (!setCookie || !cookies) return;
|
|
1565
1770
|
setCookie = [].concat(setCookie);
|
|
1566
|
-
var cookie;
|
|
1567
1771
|
|
|
1568
1772
|
for (var i = 0, n = setCookie.length; i < n; i++) {
|
|
1569
|
-
cookie =
|
|
1570
|
-
cookie
|
|
1571
|
-
cookie.domain = cookie.domain || this.endpoint.hostname;
|
|
1773
|
+
cookie = Faye.Cookies.Cookie.parse(setCookie[i]);
|
|
1774
|
+
cookies.setCookieSync(cookie, url);
|
|
1572
1775
|
}
|
|
1573
1776
|
}
|
|
1574
1777
|
|
|
1575
1778
|
}), {
|
|
1576
|
-
get: function(
|
|
1577
|
-
var endpoint =
|
|
1779
|
+
get: function(dispatcher, allowed, disabled, callback, context) {
|
|
1780
|
+
var endpoint = dispatcher.endpoint;
|
|
1578
1781
|
|
|
1579
1782
|
Faye.asyncEach(this._transports, function(pair, resume) {
|
|
1580
1783
|
var connType = pair[0], klass = pair[1],
|
|
1581
|
-
connEndpoint =
|
|
1784
|
+
connEndpoint = dispatcher.endpointFor(connType);
|
|
1582
1785
|
|
|
1583
1786
|
if (Faye.indexOf(disabled, connType) >= 0)
|
|
1584
1787
|
return resume();
|
|
1585
1788
|
|
|
1586
1789
|
if (Faye.indexOf(allowed, connType) < 0) {
|
|
1587
|
-
klass.isUsable(
|
|
1790
|
+
klass.isUsable(dispatcher, connEndpoint, function() {});
|
|
1588
1791
|
return resume();
|
|
1589
1792
|
}
|
|
1590
1793
|
|
|
1591
|
-
klass.isUsable(
|
|
1794
|
+
klass.isUsable(dispatcher, connEndpoint, function(isUsable) {
|
|
1592
1795
|
if (!isUsable) return resume();
|
|
1593
|
-
var transport = klass.hasOwnProperty('create') ? klass.create(
|
|
1796
|
+
var transport = klass.hasOwnProperty('create') ? klass.create(dispatcher, connEndpoint) : new klass(dispatcher, connEndpoint);
|
|
1594
1797
|
callback.call(context, transport);
|
|
1595
1798
|
});
|
|
1596
1799
|
}, function() {
|
|
@@ -1603,6 +1806,10 @@ Faye.Transport = Faye.extend(Faye.Class({
|
|
|
1603
1806
|
klass.prototype.connectionType = type;
|
|
1604
1807
|
},
|
|
1605
1808
|
|
|
1809
|
+
getConnectionTypes: function() {
|
|
1810
|
+
return Faye.map(this._transports, function(t) { return t[0] });
|
|
1811
|
+
},
|
|
1812
|
+
|
|
1606
1813
|
_transports: []
|
|
1607
1814
|
});
|
|
1608
1815
|
|
|
@@ -2154,16 +2361,23 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2154
2361
|
this.connect();
|
|
2155
2362
|
},
|
|
2156
2363
|
|
|
2157
|
-
request: function(
|
|
2364
|
+
request: function(messages) {
|
|
2158
2365
|
this._pending = this._pending || new Faye.Set();
|
|
2159
|
-
for (var i = 0, n =
|
|
2366
|
+
for (var i = 0, n = messages.length; i < n; i++) this._pending.add(messages[i]);
|
|
2367
|
+
|
|
2368
|
+
var promise = new Faye.Promise();
|
|
2160
2369
|
|
|
2161
2370
|
this.callback(function(socket) {
|
|
2162
|
-
if (!socket) return;
|
|
2163
|
-
var messages = Faye.map(envelopes, function(e) { return e.message });
|
|
2371
|
+
if (!socket || socket.readyState !== 1) return;
|
|
2164
2372
|
socket.send(Faye.toJSON(messages));
|
|
2373
|
+
Faye.Promise.fulfill(promise, socket);
|
|
2165
2374
|
}, this);
|
|
2375
|
+
|
|
2166
2376
|
this.connect();
|
|
2377
|
+
|
|
2378
|
+
return {
|
|
2379
|
+
abort: function() { promise.then(function(ws) { ws.close() }) }
|
|
2380
|
+
};
|
|
2167
2381
|
},
|
|
2168
2382
|
|
|
2169
2383
|
connect: function() {
|
|
@@ -2204,28 +2418,25 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2204
2418
|
delete self._pending;
|
|
2205
2419
|
|
|
2206
2420
|
if (wasConnected) {
|
|
2207
|
-
self.
|
|
2421
|
+
self._handleError(pending, true);
|
|
2208
2422
|
} else if (self._everConnected) {
|
|
2209
|
-
self.
|
|
2423
|
+
self._handleError(pending);
|
|
2210
2424
|
} else {
|
|
2211
2425
|
self.setDeferredStatus('failed');
|
|
2212
2426
|
}
|
|
2213
2427
|
};
|
|
2214
2428
|
|
|
2215
2429
|
socket.onmessage = function(event) {
|
|
2216
|
-
var
|
|
2217
|
-
|
|
2218
|
-
envelope;
|
|
2430
|
+
var replies = JSON.parse(event.data);
|
|
2431
|
+
if (!replies) return;
|
|
2219
2432
|
|
|
2220
|
-
|
|
2221
|
-
messages = [].concat(messages);
|
|
2433
|
+
replies = [].concat(replies);
|
|
2222
2434
|
|
|
2223
|
-
for (var i = 0, n =
|
|
2224
|
-
if (
|
|
2225
|
-
|
|
2226
|
-
if (envelope) envelopes.push(envelope);
|
|
2435
|
+
for (var i = 0, n = replies.length; i < n; i++) {
|
|
2436
|
+
if (replies[i].successful === undefined) continue;
|
|
2437
|
+
self._pending.remove(replies[i]);
|
|
2227
2438
|
}
|
|
2228
|
-
self.
|
|
2439
|
+
self._receive(replies);
|
|
2229
2440
|
};
|
|
2230
2441
|
},
|
|
2231
2442
|
|
|
@@ -2235,10 +2446,14 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2235
2446
|
},
|
|
2236
2447
|
|
|
2237
2448
|
_createSocket: function() {
|
|
2238
|
-
var url
|
|
2239
|
-
|
|
2449
|
+
var url = Faye.Transport.WebSocket.getSocketUrl(this.endpoint),
|
|
2450
|
+
headers = this._dispatcher.headers,
|
|
2451
|
+
extensions = this._dispatcher.wsExtensions,
|
|
2452
|
+
cookie = this._getCookies(),
|
|
2453
|
+
tls = this._dispatcher.tls,
|
|
2454
|
+
options = {extensions: extensions, headers: headers, proxy: this._proxy, tls: tls};
|
|
2240
2455
|
|
|
2241
|
-
options.headers['Cookie'] =
|
|
2456
|
+
if (cookie !== '') options.headers['Cookie'] = cookie;
|
|
2242
2457
|
|
|
2243
2458
|
if (Faye.WebSocket) return new Faye.WebSocket.Client(url, [], options);
|
|
2244
2459
|
if (Faye.ENV.MozWebSocket) return new MozWebSocket(url);
|
|
@@ -2248,7 +2463,7 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2248
2463
|
_ping: function() {
|
|
2249
2464
|
if (!this._socket) return;
|
|
2250
2465
|
this._socket.send('[]');
|
|
2251
|
-
this.addTimeout('ping', this.
|
|
2466
|
+
this.addTimeout('ping', this._dispatcher.timeout / 2, this._ping, this);
|
|
2252
2467
|
}
|
|
2253
2468
|
|
|
2254
2469
|
}), {
|
|
@@ -2257,9 +2472,9 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2257
2472
|
'https:': 'wss:'
|
|
2258
2473
|
},
|
|
2259
2474
|
|
|
2260
|
-
create: function(
|
|
2261
|
-
var sockets =
|
|
2262
|
-
sockets[endpoint.href] = sockets[endpoint.href] || new this(
|
|
2475
|
+
create: function(dispatcher, endpoint) {
|
|
2476
|
+
var sockets = dispatcher.transports.websocket = dispatcher.transports.websocket || {};
|
|
2477
|
+
sockets[endpoint.href] = sockets[endpoint.href] || new this(dispatcher, endpoint);
|
|
2263
2478
|
return sockets[endpoint.href];
|
|
2264
2479
|
},
|
|
2265
2480
|
|
|
@@ -2269,28 +2484,28 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2269
2484
|
return Faye.URI.stringify(endpoint);
|
|
2270
2485
|
},
|
|
2271
2486
|
|
|
2272
|
-
isUsable: function(
|
|
2273
|
-
this.create(
|
|
2487
|
+
isUsable: function(dispatcher, endpoint, callback, context) {
|
|
2488
|
+
this.create(dispatcher, endpoint).isUsable(callback, context);
|
|
2274
2489
|
}
|
|
2275
2490
|
});
|
|
2276
2491
|
|
|
2277
2492
|
Faye.extend(Faye.Transport.WebSocket.prototype, Faye.Deferrable);
|
|
2278
2493
|
Faye.Transport.register('websocket', Faye.Transport.WebSocket);
|
|
2279
2494
|
|
|
2280
|
-
if (Faye.Event)
|
|
2495
|
+
if (Faye.Event && Faye.ENV.onbeforeunload !== undefined)
|
|
2281
2496
|
Faye.Event.on(Faye.ENV, 'beforeunload', function() {
|
|
2282
2497
|
Faye.Transport.WebSocket._unloaded = true;
|
|
2283
2498
|
});
|
|
2284
2499
|
|
|
2285
2500
|
Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
|
|
2286
|
-
initialize: function(
|
|
2287
|
-
Faye.Transport.prototype.initialize.call(this,
|
|
2501
|
+
initialize: function(dispatcher, endpoint) {
|
|
2502
|
+
Faye.Transport.prototype.initialize.call(this, dispatcher, endpoint);
|
|
2288
2503
|
if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed');
|
|
2289
2504
|
|
|
2290
|
-
this._xhr = new Faye.Transport.XHR(
|
|
2505
|
+
this._xhr = new Faye.Transport.XHR(dispatcher, endpoint);
|
|
2291
2506
|
|
|
2292
2507
|
endpoint = Faye.copyObject(endpoint);
|
|
2293
|
-
endpoint.pathname += '/' +
|
|
2508
|
+
endpoint.pathname += '/' + dispatcher.clientId;
|
|
2294
2509
|
|
|
2295
2510
|
var socket = new EventSource(Faye.URI.stringify(endpoint)),
|
|
2296
2511
|
self = this;
|
|
@@ -2302,7 +2517,7 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2302
2517
|
|
|
2303
2518
|
socket.onerror = function() {
|
|
2304
2519
|
if (self._everConnected) {
|
|
2305
|
-
self.
|
|
2520
|
+
self._handleError([]);
|
|
2306
2521
|
} else {
|
|
2307
2522
|
self.setDeferredStatus('failed');
|
|
2308
2523
|
socket.close();
|
|
@@ -2310,7 +2525,7 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2310
2525
|
};
|
|
2311
2526
|
|
|
2312
2527
|
socket.onmessage = function(event) {
|
|
2313
|
-
self.
|
|
2528
|
+
self._receive(JSON.parse(event.data));
|
|
2314
2529
|
};
|
|
2315
2530
|
|
|
2316
2531
|
this._socket = socket;
|
|
@@ -2328,34 +2543,34 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2328
2543
|
this.errback(function() { callback.call(context, false) });
|
|
2329
2544
|
},
|
|
2330
2545
|
|
|
2331
|
-
encode: function(
|
|
2332
|
-
return this._xhr.encode(
|
|
2546
|
+
encode: function(messages) {
|
|
2547
|
+
return this._xhr.encode(messages);
|
|
2333
2548
|
},
|
|
2334
2549
|
|
|
2335
|
-
request: function(
|
|
2336
|
-
this._xhr.request(
|
|
2550
|
+
request: function(messages) {
|
|
2551
|
+
return this._xhr.request(messages);
|
|
2337
2552
|
}
|
|
2338
2553
|
|
|
2339
2554
|
}), {
|
|
2340
|
-
isUsable: function(
|
|
2341
|
-
var id =
|
|
2555
|
+
isUsable: function(dispatcher, endpoint, callback, context) {
|
|
2556
|
+
var id = dispatcher.clientId;
|
|
2342
2557
|
if (!id) return callback.call(context, false);
|
|
2343
2558
|
|
|
2344
|
-
Faye.Transport.XHR.isUsable(
|
|
2559
|
+
Faye.Transport.XHR.isUsable(dispatcher, endpoint, function(usable) {
|
|
2345
2560
|
if (!usable) return callback.call(context, false);
|
|
2346
|
-
this.create(
|
|
2561
|
+
this.create(dispatcher, endpoint).isUsable(callback, context);
|
|
2347
2562
|
}, this);
|
|
2348
2563
|
},
|
|
2349
2564
|
|
|
2350
|
-
create: function(
|
|
2351
|
-
var sockets =
|
|
2352
|
-
id =
|
|
2565
|
+
create: function(dispatcher, endpoint) {
|
|
2566
|
+
var sockets = dispatcher.transports.eventsource = dispatcher.transports.eventsource || {},
|
|
2567
|
+
id = dispatcher.clientId;
|
|
2353
2568
|
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2569
|
+
var url = Faye.copyObject(endpoint);
|
|
2570
|
+
url.pathname += '/' + (id || '');
|
|
2571
|
+
url = Faye.URI.stringify(url);
|
|
2357
2572
|
|
|
2358
|
-
sockets[url] = sockets[url] || new this(
|
|
2573
|
+
sockets[url] = sockets[url] || new this(dispatcher, endpoint);
|
|
2359
2574
|
return sockets[url];
|
|
2360
2575
|
}
|
|
2361
2576
|
});
|
|
@@ -2364,58 +2579,58 @@ Faye.extend(Faye.Transport.EventSource.prototype, Faye.Deferrable);
|
|
|
2364
2579
|
Faye.Transport.register('eventsource', Faye.Transport.EventSource);
|
|
2365
2580
|
|
|
2366
2581
|
Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, {
|
|
2367
|
-
encode: function(
|
|
2368
|
-
var messages = Faye.map(envelopes, function(e) { return e.message });
|
|
2582
|
+
encode: function(messages) {
|
|
2369
2583
|
return Faye.toJSON(messages);
|
|
2370
2584
|
},
|
|
2371
2585
|
|
|
2372
|
-
request: function(
|
|
2373
|
-
var
|
|
2586
|
+
request: function(messages) {
|
|
2587
|
+
var href = this.endpoint.href,
|
|
2374
2588
|
xhr = Faye.ENV.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(),
|
|
2375
2589
|
self = this;
|
|
2376
2590
|
|
|
2377
|
-
xhr.open('POST',
|
|
2591
|
+
xhr.open('POST', href, true);
|
|
2378
2592
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
2379
2593
|
xhr.setRequestHeader('Pragma', 'no-cache');
|
|
2380
2594
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
|
2381
2595
|
|
|
2382
|
-
var headers = this.
|
|
2596
|
+
var headers = this._dispatcher.headers;
|
|
2383
2597
|
for (var key in headers) {
|
|
2384
2598
|
if (!headers.hasOwnProperty(key)) continue;
|
|
2385
2599
|
xhr.setRequestHeader(key, headers[key]);
|
|
2386
2600
|
}
|
|
2387
2601
|
|
|
2388
2602
|
var abort = function() { xhr.abort() };
|
|
2389
|
-
Faye.Event.on(Faye.ENV, 'beforeunload', abort);
|
|
2603
|
+
if (Faye.ENV.onbeforeunload !== undefined) Faye.Event.on(Faye.ENV, 'beforeunload', abort);
|
|
2390
2604
|
|
|
2391
2605
|
xhr.onreadystatechange = function() {
|
|
2392
2606
|
if (!xhr || xhr.readyState !== 4) return;
|
|
2393
2607
|
|
|
2394
|
-
var
|
|
2395
|
-
status
|
|
2396
|
-
text
|
|
2397
|
-
successful
|
|
2608
|
+
var replies = null,
|
|
2609
|
+
status = xhr.status,
|
|
2610
|
+
text = xhr.responseText,
|
|
2611
|
+
successful = (status >= 200 && status < 300) || status === 304 || status === 1223;
|
|
2398
2612
|
|
|
2399
|
-
Faye.Event.detach(Faye.ENV, 'beforeunload', abort);
|
|
2613
|
+
if (Faye.ENV.onbeforeunload !== undefined) Faye.Event.detach(Faye.ENV, 'beforeunload', abort);
|
|
2400
2614
|
xhr.onreadystatechange = function() {};
|
|
2401
2615
|
xhr = null;
|
|
2402
2616
|
|
|
2403
|
-
if (!successful) return self.
|
|
2617
|
+
if (!successful) return self._handleError(messages);
|
|
2404
2618
|
|
|
2405
2619
|
try {
|
|
2406
|
-
|
|
2620
|
+
replies = JSON.parse(text);
|
|
2407
2621
|
} catch (e) {}
|
|
2408
2622
|
|
|
2409
|
-
if (
|
|
2410
|
-
self.
|
|
2623
|
+
if (replies)
|
|
2624
|
+
self._receive(replies);
|
|
2411
2625
|
else
|
|
2412
|
-
self.
|
|
2626
|
+
self._handleError(messages);
|
|
2413
2627
|
};
|
|
2414
2628
|
|
|
2415
|
-
xhr.send(this.encode(
|
|
2629
|
+
xhr.send(this.encode(messages));
|
|
2630
|
+
return xhr;
|
|
2416
2631
|
}
|
|
2417
2632
|
}), {
|
|
2418
|
-
isUsable: function(
|
|
2633
|
+
isUsable: function(dispatcher, endpoint, callback, context) {
|
|
2419
2634
|
callback.call(context, Faye.URI.isSameOrigin(endpoint));
|
|
2420
2635
|
}
|
|
2421
2636
|
});
|
|
@@ -2423,15 +2638,15 @@ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2423
2638
|
Faye.Transport.register('long-polling', Faye.Transport.XHR);
|
|
2424
2639
|
|
|
2425
2640
|
Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
|
|
2426
|
-
encode: function(
|
|
2427
|
-
var messages = Faye.map(envelopes, function(e) { return e.message });
|
|
2641
|
+
encode: function(messages) {
|
|
2428
2642
|
return 'message=' + encodeURIComponent(Faye.toJSON(messages));
|
|
2429
2643
|
},
|
|
2430
2644
|
|
|
2431
|
-
request: function(
|
|
2645
|
+
request: function(messages) {
|
|
2432
2646
|
var xhrClass = Faye.ENV.XDomainRequest ? XDomainRequest : XMLHttpRequest,
|
|
2433
2647
|
xhr = new xhrClass(),
|
|
2434
|
-
|
|
2648
|
+
id = ++Faye.Transport.CORS._id,
|
|
2649
|
+
headers = this._dispatcher.headers,
|
|
2435
2650
|
self = this,
|
|
2436
2651
|
key;
|
|
2437
2652
|
|
|
@@ -2447,34 +2662,43 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2447
2662
|
|
|
2448
2663
|
var cleanUp = function() {
|
|
2449
2664
|
if (!xhr) return false;
|
|
2665
|
+
Faye.Transport.CORS._pending.remove(id);
|
|
2450
2666
|
xhr.onload = xhr.onerror = xhr.ontimeout = xhr.onprogress = null;
|
|
2451
2667
|
xhr = null;
|
|
2452
2668
|
};
|
|
2453
2669
|
|
|
2454
2670
|
xhr.onload = function() {
|
|
2455
|
-
var
|
|
2671
|
+
var replies = null;
|
|
2456
2672
|
try {
|
|
2457
|
-
|
|
2673
|
+
replies = JSON.parse(xhr.responseText);
|
|
2458
2674
|
} catch (e) {}
|
|
2459
2675
|
|
|
2460
2676
|
cleanUp();
|
|
2461
2677
|
|
|
2462
|
-
if (
|
|
2463
|
-
self.
|
|
2678
|
+
if (replies)
|
|
2679
|
+
self._receive(replies);
|
|
2464
2680
|
else
|
|
2465
|
-
self.
|
|
2681
|
+
self._handleError(messages);
|
|
2466
2682
|
};
|
|
2467
2683
|
|
|
2468
2684
|
xhr.onerror = xhr.ontimeout = function() {
|
|
2469
2685
|
cleanUp();
|
|
2470
|
-
self.
|
|
2686
|
+
self._handleError(messages);
|
|
2471
2687
|
};
|
|
2472
2688
|
|
|
2473
2689
|
xhr.onprogress = function() {};
|
|
2474
|
-
|
|
2690
|
+
|
|
2691
|
+
if (xhrClass === Faye.ENV.XDomainRequest)
|
|
2692
|
+
Faye.Transport.CORS._pending.add({id: id, xhr: xhr});
|
|
2693
|
+
|
|
2694
|
+
xhr.send(this.encode(messages));
|
|
2695
|
+
return xhr;
|
|
2475
2696
|
}
|
|
2476
2697
|
}), {
|
|
2477
|
-
|
|
2698
|
+
_id: 0,
|
|
2699
|
+
_pending: new Faye.Set(),
|
|
2700
|
+
|
|
2701
|
+
isUsable: function(dispatcher, endpoint, callback, context) {
|
|
2478
2702
|
if (Faye.URI.isSameOrigin(endpoint))
|
|
2479
2703
|
return callback.call(context, false);
|
|
2480
2704
|
|
|
@@ -2492,17 +2716,15 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2492
2716
|
Faye.Transport.register('cross-origin-long-polling', Faye.Transport.CORS);
|
|
2493
2717
|
|
|
2494
2718
|
Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
|
|
2495
|
-
encode: function(
|
|
2496
|
-
var messages = Faye.map(envelopes, function(e) { return e.message });
|
|
2719
|
+
encode: function(messages) {
|
|
2497
2720
|
var url = Faye.copyObject(this.endpoint);
|
|
2498
2721
|
url.query.message = Faye.toJSON(messages);
|
|
2499
2722
|
url.query.jsonp = '__jsonp' + Faye.Transport.JSONP._cbCount + '__';
|
|
2500
2723
|
return Faye.URI.stringify(url);
|
|
2501
2724
|
},
|
|
2502
2725
|
|
|
2503
|
-
request: function(
|
|
2504
|
-
var
|
|
2505
|
-
head = document.getElementsByTagName('head')[0],
|
|
2726
|
+
request: function(messages) {
|
|
2727
|
+
var head = document.getElementsByTagName('head')[0],
|
|
2506
2728
|
script = document.createElement('script'),
|
|
2507
2729
|
callbackName = Faye.Transport.JSONP.getCallbackName(),
|
|
2508
2730
|
endpoint = Faye.copyObject(this.endpoint),
|
|
@@ -2511,17 +2733,28 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2511
2733
|
endpoint.query.message = Faye.toJSON(messages);
|
|
2512
2734
|
endpoint.query.jsonp = callbackName;
|
|
2513
2735
|
|
|
2514
|
-
|
|
2736
|
+
var cleanup = function() {
|
|
2515
2737
|
if (!Faye.ENV[callbackName]) return false;
|
|
2516
2738
|
Faye.ENV[callbackName] = undefined;
|
|
2517
2739
|
try { delete Faye.ENV[callbackName] } catch (e) {}
|
|
2518
2740
|
script.parentNode.removeChild(script);
|
|
2519
|
-
|
|
2741
|
+
};
|
|
2742
|
+
|
|
2743
|
+
Faye.ENV[callbackName] = function(replies) {
|
|
2744
|
+
cleanup();
|
|
2745
|
+
self._receive(replies);
|
|
2520
2746
|
};
|
|
2521
2747
|
|
|
2522
2748
|
script.type = 'text/javascript';
|
|
2523
2749
|
script.src = Faye.URI.stringify(endpoint);
|
|
2524
2750
|
head.appendChild(script);
|
|
2751
|
+
|
|
2752
|
+
script.onerror = function() {
|
|
2753
|
+
cleanup();
|
|
2754
|
+
self._handleError(messages);
|
|
2755
|
+
};
|
|
2756
|
+
|
|
2757
|
+
return {abort: cleanup};
|
|
2525
2758
|
}
|
|
2526
2759
|
}), {
|
|
2527
2760
|
_cbCount: 0,
|
|
@@ -2531,7 +2764,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
|
|
|
2531
2764
|
return '__jsonp' + this._cbCount + '__';
|
|
2532
2765
|
},
|
|
2533
2766
|
|
|
2534
|
-
isUsable: function(
|
|
2767
|
+
isUsable: function(dispatcher, endpoint, callback, context) {
|
|
2535
2768
|
callback.call(context, true);
|
|
2536
2769
|
}
|
|
2537
2770
|
});
|