faye 0.8.5 → 0.8.11

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.
@@ -1,29 +1,28 @@
1
- var Faye = (typeof Faye === 'object') ? Faye : {};
2
- if (typeof window !== 'undefined') window.Faye = Faye;
3
-
4
- Faye.extend = function(dest, source, overwrite) {
5
- if (!source) return dest;
6
- for (var key in source) {
7
- if (!source.hasOwnProperty(key)) continue;
8
- if (dest.hasOwnProperty(key) && overwrite === false) continue;
9
- if (dest[key] !== source[key])
10
- dest[key] = source[key];
11
- }
12
- return dest;
13
- };
1
+ 'use strict';
2
+
3
+ var Faye = {
4
+ VERSION: '0.8.11',
14
5
 
15
- Faye.extend(Faye, {
16
- VERSION: '0.8.5',
17
-
18
6
  BAYEUX_VERSION: '1.0',
19
7
  ID_LENGTH: 160,
20
8
  JSONP_CALLBACK: 'jsonpcallback',
21
9
  CONNECTION_TYPES: ['long-polling', 'cross-origin-long-polling', 'callback-polling', 'websocket', 'eventsource', 'in-process'],
22
-
10
+
23
11
  MANDATORY_CONNECTION_TYPES: ['long-polling', 'callback-polling', 'in-process'],
24
-
25
- ENV: (function() { return this })(),
26
-
12
+
13
+ ENV: (typeof global === 'undefined') ? window : global,
14
+
15
+ extend: function(dest, source, overwrite) {
16
+ if (!source) return dest;
17
+ for (var key in source) {
18
+ if (!source.hasOwnProperty(key)) continue;
19
+ if (dest.hasOwnProperty(key) && overwrite === false) continue;
20
+ if (dest[key] !== source[key])
21
+ dest[key] = source[key];
22
+ }
23
+ return dest;
24
+ },
25
+
27
26
  random: function(bitlength) {
28
27
  bitlength = bitlength || this.ID_LENGTH;
29
28
  if (bitlength > 32) {
@@ -37,16 +36,16 @@ Faye.extend(Faye, {
37
36
  var limit = Math.pow(2, bitlength) - 1,
38
37
  maxSize = limit.toString(36).length,
39
38
  string = Math.floor(Math.random() * limit).toString(36);
40
-
39
+
41
40
  while (string.length < maxSize) string = '0' + string;
42
41
  return string;
43
42
  },
44
-
43
+
45
44
  clientIdFromMessages: function(messages) {
46
45
  var first = [].concat(messages)[0];
47
46
  return first && first.clientId;
48
47
  },
49
-
48
+
50
49
  copyObject: function(object) {
51
50
  var clone, i, key;
52
51
  if (object instanceof Array) {
@@ -62,7 +61,7 @@ Faye.extend(Faye, {
62
61
  return object;
63
62
  }
64
63
  },
65
-
64
+
66
65
  commonElement: function(lista, listb) {
67
66
  for (var i = 0, n = lista.length; i < n; i++) {
68
67
  if (this.indexOf(listb, lista[i]) !== -1)
@@ -70,20 +69,20 @@ Faye.extend(Faye, {
70
69
  }
71
70
  return null;
72
71
  },
73
-
72
+
74
73
  indexOf: function(list, needle) {
75
74
  if (list.indexOf) return list.indexOf(needle);
76
-
75
+
77
76
  for (var i = 0, n = list.length; i < n; i++) {
78
77
  if (list[i] === needle) return i;
79
78
  }
80
79
  return -1;
81
80
  },
82
-
81
+
83
82
  map: function(object, callback, context) {
84
83
  if (object.map) return object.map(callback, context);
85
84
  var result = [];
86
-
85
+
87
86
  if (object instanceof Array) {
88
87
  for (var i = 0, n = object.length; i < n; i++) {
89
88
  result.push(callback.call(context || null, object[i], i));
@@ -96,7 +95,7 @@ Faye.extend(Faye, {
96
95
  }
97
96
  return result;
98
97
  },
99
-
98
+
100
99
  filter: function(array, callback, context) {
101
100
  var result = [];
102
101
  for (var i = 0, n = array.length; i < n; i++) {
@@ -105,7 +104,7 @@ Faye.extend(Faye, {
105
104
  }
106
105
  return result;
107
106
  },
108
-
107
+
109
108
  asyncEach: function(list, iterator, callback, context) {
110
109
  var n = list.length,
111
110
  i = -1,
@@ -132,7 +131,7 @@ Faye.extend(Faye, {
132
131
  };
133
132
  resume();
134
133
  },
135
-
134
+
136
135
  // http://assanka.net/content/tech/2009/09/02/json2-js-vs-prototype/
137
136
  toJSON: function(object) {
138
137
  if (this.stringify)
@@ -141,14 +140,14 @@ Faye.extend(Faye, {
141
140
  ? this[key]
142
141
  : value;
143
142
  });
144
-
143
+
145
144
  return JSON.stringify(object);
146
145
  },
147
-
146
+
148
147
  logger: function(message) {
149
148
  if (typeof console !== 'undefined') console.log(message);
150
149
  },
151
-
150
+
152
151
  timestamp: function() {
153
152
  var date = new Date(),
154
153
  year = date.getFullYear(),
@@ -157,15 +156,18 @@ Faye.extend(Faye, {
157
156
  hour = date.getHours(),
158
157
  minute = date.getMinutes(),
159
158
  second = date.getSeconds();
160
-
159
+
161
160
  var pad = function(n) {
162
161
  return n < 10 ? '0' + n : String(n);
163
162
  };
164
-
163
+
165
164
  return pad(year) + '-' + pad(month) + '-' + pad(day) + ' ' +
166
165
  pad(hour) + ':' + pad(minute) + ':' + pad(second);
167
166
  }
168
- });
167
+ };
168
+
169
+ if (typeof window !== 'undefined')
170
+ window.Faye = Faye;
169
171
 
170
172
 
171
173
  Faye.Class = function(parent, methods) {
@@ -173,18 +175,18 @@ Faye.Class = function(parent, methods) {
173
175
  methods = parent;
174
176
  parent = Object;
175
177
  }
176
-
178
+
177
179
  var klass = function() {
178
180
  if (!this.initialize) return this;
179
181
  return this.initialize.apply(this, arguments) || this;
180
182
  };
181
-
183
+
182
184
  var bridge = function() {};
183
185
  bridge.prototype = parent.prototype;
184
-
186
+
185
187
  klass.prototype = new bridge();
186
188
  Faye.extend(klass.prototype, methods);
187
-
189
+
188
190
  return klass;
189
191
  };
190
192
 
@@ -193,18 +195,18 @@ Faye.Namespace = Faye.Class({
193
195
  initialize: function() {
194
196
  this._used = {};
195
197
  },
196
-
198
+
197
199
  exists: function(id) {
198
200
  return this._used.hasOwnProperty(id);
199
201
  },
200
-
202
+
201
203
  generate: function() {
202
204
  var name = Faye.random();
203
205
  while (this._used.hasOwnProperty(name))
204
206
  name = Faye.random();
205
207
  return this._used[name] = name;
206
208
  },
207
-
209
+
208
210
  release: function(id) {
209
211
  delete this._used[id];
210
212
  }
@@ -217,7 +219,7 @@ Faye.Error = Faye.Class({
217
219
  this.params = Array.prototype.slice.call(params);
218
220
  this.message = message;
219
221
  },
220
-
222
+
221
223
  toString: function() {
222
224
  return this.code + ':' +
223
225
  this.params.join(',') + ':' +
@@ -291,14 +293,14 @@ Faye.Error.serverError = function() {
291
293
  Faye.Deferrable = {
292
294
  callback: function(callback, context) {
293
295
  if (!callback) return;
294
-
296
+
295
297
  if (this._deferredStatus === 'succeeded')
296
298
  return callback.apply(context, this._deferredArgs);
297
-
299
+
298
300
  this._callbacks = this._callbacks || [];
299
301
  this._callbacks.push([callback, context]);
300
302
  },
301
-
303
+
302
304
  timeout: function(seconds, message) {
303
305
  var _this = this;
304
306
  var timer = Faye.ENV.setTimeout(function() {
@@ -306,7 +308,7 @@ Faye.Deferrable = {
306
308
  }, seconds * 1000);
307
309
  this._timer = timer;
308
310
  },
309
-
311
+
310
312
  errback: function(callback, context) {
311
313
  if (!callback) return;
312
314
 
@@ -324,17 +326,17 @@ Faye.Deferrable = {
324
326
  var args = Array.prototype.slice.call(arguments),
325
327
  status = args.shift(),
326
328
  callbacks;
327
-
329
+
328
330
  this._deferredStatus = status;
329
331
  this._deferredArgs = args;
330
-
332
+
331
333
  if (status === 'succeeded')
332
334
  callbacks = this._callbacks;
333
335
  else if (status === 'failed')
334
336
  callbacks = this._errbacks;
335
-
337
+
336
338
  if (!callbacks) return;
337
-
339
+
338
340
  var callback;
339
341
  while (callback = callbacks.shift())
340
342
  callback[0].apply(callback[1], this._deferredArgs);
@@ -347,39 +349,39 @@ Faye.Publisher = {
347
349
  if (!this._subscribers || !this._subscribers[eventType]) return 0;
348
350
  return this._subscribers[eventType].length;
349
351
  },
350
-
352
+
351
353
  bind: function(eventType, listener, context) {
352
354
  this._subscribers = this._subscribers || {};
353
355
  var list = this._subscribers[eventType] = this._subscribers[eventType] || [];
354
356
  list.push([listener, context]);
355
357
  },
356
-
358
+
357
359
  unbind: function(eventType, listener, context) {
358
360
  if (!this._subscribers || !this._subscribers[eventType]) return;
359
-
361
+
360
362
  if (!listener) {
361
363
  delete this._subscribers[eventType];
362
364
  return;
363
365
  }
364
366
  var list = this._subscribers[eventType],
365
367
  i = list.length;
366
-
368
+
367
369
  while (i--) {
368
370
  if (listener !== list[i][0]) continue;
369
371
  if (context && list[i][1] !== context) continue;
370
372
  list.splice(i,1);
371
373
  }
372
374
  },
373
-
375
+
374
376
  trigger: function() {
375
377
  var args = Array.prototype.slice.call(arguments),
376
378
  eventType = args.shift();
377
-
379
+
378
380
  if (!this._subscribers || !this._subscribers[eventType]) return;
379
-
381
+
380
382
  var listeners = this._subscribers[eventType].slice(),
381
383
  listener;
382
-
384
+
383
385
  for (var i = 0, n = listeners.length; i < n; i++) {
384
386
  listener = listeners[i];
385
387
  listener[0].apply(listener[1], args);
@@ -398,7 +400,7 @@ Faye.Timeouts = {
398
400
  callback.call(context);
399
401
  }, 1000 * delay);
400
402
  },
401
-
403
+
402
404
  removeTimeout: function(name) {
403
405
  this._timeouts = this._timeouts || {};
404
406
  var timeout = this._timeouts[name];
@@ -416,19 +418,19 @@ Faye.Logging = {
416
418
  info: 1,
417
419
  debug: 0
418
420
  },
419
-
421
+
420
422
  logLevel: 'error',
421
-
423
+
422
424
  log: function(messageArgs, level) {
423
425
  if (!Faye.logger) return;
424
-
426
+
425
427
  var levels = Faye.Logging.LOG_LEVELS;
426
428
  if (levels[Faye.Logging.logLevel] > levels[level]) return;
427
-
429
+
428
430
  var messageArgs = Array.prototype.slice.apply(messageArgs),
429
431
  banner = ' [' + level.toUpperCase() + '] [Faye',
430
432
  klass = this.className,
431
-
433
+
432
434
  message = messageArgs.shift().replace(/\?/g, function() {
433
435
  try {
434
436
  return Faye.toJSON(messageArgs.shift());
@@ -436,7 +438,7 @@ Faye.Logging = {
436
438
  return '[Object]';
437
439
  }
438
440
  });
439
-
441
+
440
442
  for (var key in Faye) {
441
443
  if (klass) continue;
442
444
  if (typeof Faye[key] !== 'function') continue;
@@ -444,7 +446,7 @@ Faye.Logging = {
444
446
  }
445
447
  if (klass) banner += '.' + klass;
446
448
  banner += '] ';
447
-
449
+
448
450
  Faye.logger(Faye.timestamp() + banner + message);
449
451
  }
450
452
  };
@@ -514,7 +516,7 @@ Faye.Extensible = {
514
516
  this._extensions.push(extension);
515
517
  if (extension.added) extension.added(this);
516
518
  },
517
-
519
+
518
520
  removeExtension: function(extension) {
519
521
  if (!this._extensions) return;
520
522
  var i = this._extensions.length;
@@ -524,19 +526,19 @@ Faye.Extensible = {
524
526
  if (extension.removed) extension.removed(this);
525
527
  }
526
528
  },
527
-
529
+
528
530
  pipeThroughExtensions: function(stage, message, callback, context) {
529
531
  this.debug('Passing through ? extensions: ?', stage, message);
530
532
 
531
533
  if (!this._extensions) return callback.call(context, message);
532
534
  var extensions = this._extensions.slice();
533
-
535
+
534
536
  var pipe = function(message) {
535
537
  if (!message) return callback.call(context, message);
536
-
538
+
537
539
  var extension = extensions.shift();
538
540
  if (!extension) return callback.call(context, message);
539
-
541
+
540
542
  if (extension[stage]) extension[stage](message, pipe);
541
543
  else pipe(message);
542
544
  };
@@ -550,11 +552,11 @@ Faye.Channel = Faye.Class({
550
552
  initialize: function(name) {
551
553
  this.id = this.name = name;
552
554
  },
553
-
555
+
554
556
  push: function(message) {
555
557
  this.trigger('message', message);
556
558
  },
557
-
559
+
558
560
  isUnused: function() {
559
561
  return this.countListeners('message') === 0;
560
562
  }
@@ -568,75 +570,75 @@ Faye.extend(Faye.Channel, {
568
570
  SUBSCRIBE: '/meta/subscribe',
569
571
  UNSUBSCRIBE: '/meta/unsubscribe',
570
572
  DISCONNECT: '/meta/disconnect',
571
-
573
+
572
574
  META: 'meta',
573
575
  SERVICE: 'service',
574
-
576
+
575
577
  expand: function(name) {
576
578
  var segments = this.parse(name),
577
579
  channels = ['/**', name];
578
-
580
+
579
581
  var copy = segments.slice();
580
582
  copy[copy.length - 1] = '*';
581
583
  channels.push(this.unparse(copy));
582
-
584
+
583
585
  for (var i = 1, n = segments.length; i < n; i++) {
584
586
  copy = segments.slice(0, i);
585
587
  copy.push('**');
586
588
  channels.push(this.unparse(copy));
587
589
  }
588
-
590
+
589
591
  return channels;
590
592
  },
591
-
593
+
592
594
  isValid: function(name) {
593
595
  return Faye.Grammar.CHANNEL_NAME.test(name) ||
594
596
  Faye.Grammar.CHANNEL_PATTERN.test(name);
595
597
  },
596
-
598
+
597
599
  parse: function(name) {
598
600
  if (!this.isValid(name)) return null;
599
601
  return name.split('/').slice(1);
600
602
  },
601
-
603
+
602
604
  unparse: function(segments) {
603
605
  return '/' + segments.join('/');
604
606
  },
605
-
607
+
606
608
  isMeta: function(name) {
607
609
  var segments = this.parse(name);
608
610
  return segments ? (segments[0] === this.META) : null;
609
611
  },
610
-
612
+
611
613
  isService: function(name) {
612
614
  var segments = this.parse(name);
613
615
  return segments ? (segments[0] === this.SERVICE) : null;
614
616
  },
615
-
617
+
616
618
  isSubscribable: function(name) {
617
619
  if (!this.isValid(name)) return null;
618
620
  return !this.isMeta(name) && !this.isService(name);
619
621
  },
620
-
622
+
621
623
  Set: Faye.Class({
622
624
  initialize: function() {
623
625
  this._channels = {};
624
626
  },
625
-
627
+
626
628
  getKeys: function() {
627
629
  var keys = [];
628
630
  for (var key in this._channels) keys.push(key);
629
631
  return keys;
630
632
  },
631
-
633
+
632
634
  remove: function(name) {
633
635
  delete this._channels[name];
634
636
  },
635
-
637
+
636
638
  hasSubscription: function(name) {
637
639
  return this._channels.hasOwnProperty(name);
638
640
  },
639
-
641
+
640
642
  subscribe: function(names, callback, context) {
641
643
  if (!callback) return;
642
644
  var name;
@@ -646,12 +648,12 @@ Faye.extend(Faye.Channel, {
646
648
  channel.bind('message', callback, context);
647
649
  }
648
650
  },
649
-
651
+
650
652
  unsubscribe: function(name, callback, context) {
651
653
  var channel = this._channels[name];
652
654
  if (!channel) return false;
653
655
  channel.unbind('message', callback, context);
654
-
656
+
655
657
  if (channel.isUnused()) {
656
658
  this.remove(name);
657
659
  return true;
@@ -659,10 +661,10 @@ Faye.extend(Faye.Channel, {
659
661
  return false;
660
662
  }
661
663
  },
662
-
664
+
663
665
  distributeMessage: function(message) {
664
666
  var channels = Faye.Channel.expand(message.channel);
665
-
667
+
666
668
  for (var i = 0, n = channels.length; i < n; i++) {
667
669
  var channel = this._channels[channels[i]];
668
670
  if (channel) channel.trigger('message', message.data);
@@ -683,13 +685,13 @@ Faye.Subscription = Faye.Class({
683
685
  this._context = context;
684
686
  this._cancelled = false;
685
687
  },
686
-
688
+
687
689
  cancel: function() {
688
690
  if (this._cancelled) return;
689
691
  this._client.unsubscribe(this._channels, this._callback, this._context);
690
692
  this._cancelled = true;
691
693
  },
692
-
694
+
693
695
  unsubscribe: function() {
694
696
  this.cancel();
695
697
  }
@@ -703,20 +705,20 @@ Faye.Client = Faye.Class({
703
705
  CONNECTING: 2,
704
706
  CONNECTED: 3,
705
707
  DISCONNECTED: 4,
706
-
708
+
707
709
  HANDSHAKE: 'handshake',
708
710
  RETRY: 'retry',
709
711
  NONE: 'none',
710
-
712
+
711
713
  CONNECTION_TIMEOUT: 60.0,
712
714
  DEFAULT_RETRY: 5.0,
713
-
715
+
714
716
  DEFAULT_ENDPOINT: '/bayeux',
715
717
  INTERVAL: 0.0,
716
-
718
+
717
719
  initialize: function(endpoint, options) {
718
720
  this.info('New client created for ?', endpoint);
719
-
721
+
720
722
  this._options = options || {};
721
723
  this.endpoint = endpoint || this.DEFAULT_ENDPOINT;
722
724
  this.endpoints = this._options.endpoints || {};
@@ -725,36 +727,34 @@ Faye.Client = Faye.Class({
725
727
  this._headers = {};
726
728
  this._disabled = [];
727
729
  this.retry = this._options.retry || this.DEFAULT_RETRY;
728
-
729
- this._selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
730
-
730
+
731
731
  this._state = this.UNCONNECTED;
732
732
  this._channels = new Faye.Channel.Set();
733
733
  this._messageId = 0;
734
-
734
+
735
735
  this._responseCallbacks = {};
736
-
736
+
737
737
  this._advice = {
738
738
  reconnect: this.RETRY,
739
739
  interval: 1000 * (this._options.interval || this.INTERVAL),
740
740
  timeout: 1000 * (this._options.timeout || this.CONNECTION_TIMEOUT)
741
741
  };
742
-
742
+
743
743
  if (Faye.Event)
744
744
  Faye.Event.on(Faye.ENV, 'beforeunload', function() {
745
745
  if (Faye.indexOf(this._disabled, 'autodisconnect') < 0)
746
746
  this.disconnect();
747
747
  }, this);
748
748
  },
749
-
749
+
750
750
  disable: function(feature) {
751
751
  this._disabled.push(feature);
752
752
  },
753
-
753
+
754
754
  setHeader: function(name, value) {
755
755
  this._headers[name] = value;
756
756
  },
757
-
757
+
758
758
  getClientId: function() {
759
759
  return this._clientId;
760
760
  },
@@ -775,7 +775,7 @@ Faye.Client = Faye.Class({
775
775
  // MAY include: * minimumVersion
776
776
  // * ext
777
777
  // * id
778
- //
778
+ //
779
779
  // Success Response Failed Response
780
780
  // MUST include: * channel MUST include: * channel
781
781
  // * version * successful
@@ -790,33 +790,31 @@ Faye.Client = Faye.Class({
790
790
  handshake: function(callback, context) {
791
791
  if (this._advice.reconnect === this.NONE) return;
792
792
  if (this._state !== this.UNCONNECTED) return;
793
-
793
+
794
794
  this._state = this.CONNECTING;
795
795
  var self = this;
796
-
796
+
797
797
  this.info('Initiating handshake with ?', this.endpoint);
798
-
798
+ this._selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
799
+
799
800
  this._send({
800
801
  channel: Faye.Channel.HANDSHAKE,
801
802
  version: Faye.BAYEUX_VERSION,
802
803
  supportedConnectionTypes: [this._transport.connectionType]
803
-
804
+
804
805
  }, function(response) {
805
-
806
+
806
807
  if (response.successful) {
807
808
  this._state = this.CONNECTED;
808
809
  this._clientId = response.clientId;
809
-
810
- var connectionTypes = Faye.filter(response.supportedConnectionTypes, function(connType) {
811
- return Faye.indexOf(this._disabled, connType) < 0;
812
- }, this);
813
- this._selectTransport(connectionTypes);
814
-
810
+
811
+ this._selectTransport(response.supportedConnectionTypes);
812
+
815
813
  this.info('Handshake successful: ?', this._clientId);
816
-
814
+
817
815
  this.subscribe(this._channels.getKeys(), true);
818
816
  if (callback) callback.call(context);
819
-
817
+
820
818
  } else {
821
819
  this.info('Handshake unsuccessful');
822
820
  Faye.ENV.setTimeout(function() { self.handshake(callback, context) }, this._advice.interval);
@@ -824,7 +822,7 @@ Faye.Client = Faye.Class({
824
822
  }
825
823
  }, this);
826
824
  },
827
-
825
+
828
826
  // Request Response
829
827
  // MUST include: * channel MUST include: * channel
830
828
  // * clientId * successful
@@ -837,30 +835,30 @@ Faye.Client = Faye.Class({
837
835
  connect: function(callback, context) {
838
836
  if (this._advice.reconnect === this.NONE) return;
839
837
  if (this._state === this.DISCONNECTED) return;
840
-
838
+
841
839
  if (this._state === this.UNCONNECTED)
842
840
  return this.handshake(function() { this.connect(callback, context) }, this);
843
-
841
+
844
842
  this.callback(callback, context);
845
843
  if (this._state !== this.CONNECTED) return;
846
-
844
+
847
845
  this.info('Calling deferred actions for ?', this._clientId);
848
846
  this.setDeferredStatus('succeeded');
849
847
  this.setDeferredStatus('deferred');
850
-
848
+
851
849
  if (this._connectRequest) return;
852
850
  this._connectRequest = true;
853
-
851
+
854
852
  this.info('Initiating connection for ?', this._clientId);
855
-
853
+
856
854
  this._send({
857
855
  channel: Faye.Channel.CONNECT,
858
856
  clientId: this._clientId,
859
857
  connectionType: this._transport.connectionType
860
-
858
+
861
859
  }, this._cycleConnection, this);
862
860
  },
863
-
861
+
864
862
  // Request Response
865
863
  // MUST include: * channel MUST include: * channel
866
864
  // * clientId * successful
@@ -871,21 +869,21 @@ Faye.Client = Faye.Class({
871
869
  disconnect: function() {
872
870
  if (this._state !== this.CONNECTED) return;
873
871
  this._state = this.DISCONNECTED;
874
-
872
+
875
873
  this.info('Disconnecting ?', this._clientId);
876
-
874
+
877
875
  this._send({
878
876
  channel: Faye.Channel.DISCONNECT,
879
877
  clientId: this._clientId
880
-
878
+
881
879
  }, function(response) {
882
880
  if (response.successful) this._transport.close();
883
881
  }, this);
884
-
882
+
885
883
  this.info('Clearing channel listeners for ?', this._clientId);
886
884
  this._channels = new Faye.Channel.Set();
887
885
  },
888
-
886
+
889
887
  // Request Response
890
888
  // MUST include: * channel MUST include: * channel
891
889
  // * clientId * successful
@@ -901,41 +899,41 @@ Faye.Client = Faye.Class({
901
899
  return Faye.map(channel, function(c) {
902
900
  return this.subscribe(c, callback, context);
903
901
  }, this);
904
-
902
+
905
903
  var subscription = new Faye.Subscription(this, channel, callback, context),
906
904
  force = (callback === true),
907
905
  hasSubscribe = this._channels.hasSubscription(channel);
908
-
906
+
909
907
  if (hasSubscribe && !force) {
910
908
  this._channels.subscribe([channel], callback, context);
911
909
  subscription.setDeferredStatus('succeeded');
912
910
  return subscription;
913
911
  }
914
-
912
+
915
913
  this.connect(function() {
916
914
  this.info('Client ? attempting to subscribe to ?', this._clientId, channel);
917
915
  if (!force) this._channels.subscribe([channel], callback, context);
918
-
916
+
919
917
  this._send({
920
918
  channel: Faye.Channel.SUBSCRIBE,
921
919
  clientId: this._clientId,
922
920
  subscription: channel
923
-
921
+
924
922
  }, function(response) {
925
923
  if (!response.successful) {
926
924
  subscription.setDeferredStatus('failed', Faye.Error.parse(response.error));
927
925
  return this._channels.unsubscribe(channel, callback, context);
928
926
  }
929
-
927
+
930
928
  var channels = [].concat(response.subscription);
931
929
  this.info('Subscription acknowledged for ? to ?', this._clientId, channels);
932
930
  subscription.setDeferredStatus('succeeded');
933
931
  }, this);
934
932
  }, this);
935
-
933
+
936
934
  return subscription;
937
935
  },
938
-
936
+
939
937
  // Request Response
940
938
  // MUST include: * channel MUST include: * channel
941
939
  // * clientId * successful
@@ -951,27 +949,27 @@ Faye.Client = Faye.Class({
951
949
  return Faye.map(channel, function(c) {
952
950
  return this.unsubscribe(c, callback, context);
953
951
  }, this);
954
-
952
+
955
953
  var dead = this._channels.unsubscribe(channel, callback, context);
956
954
  if (!dead) return;
957
-
955
+
958
956
  this.connect(function() {
959
957
  this.info('Client ? attempting to unsubscribe from ?', this._clientId, channel);
960
-
958
+
961
959
  this._send({
962
960
  channel: Faye.Channel.UNSUBSCRIBE,
963
961
  clientId: this._clientId,
964
962
  subscription: channel
965
-
963
+
966
964
  }, function(response) {
967
965
  if (!response.successful) return;
968
-
966
+
969
967
  var channels = [].concat(response.subscription);
970
968
  this.info('Unsubscription acknowledged for ? from ?', this._clientId, channels);
971
969
  }, this);
972
970
  }, this);
973
971
  },
974
-
972
+
975
973
  // Request Response
976
974
  // MUST include: * channel MUST include: * channel
977
975
  // * data * successful
@@ -980,10 +978,10 @@ Faye.Client = Faye.Class({
980
978
  // * ext * ext
981
979
  publish: function(channel, data) {
982
980
  var publication = new Faye.Publication();
983
-
981
+
984
982
  this.connect(function() {
985
983
  this.info('Client ? queueing published message to ?: ?', this._clientId, channel, data);
986
-
984
+
987
985
  this._send({
988
986
  channel: channel,
989
987
  data: data,
@@ -995,41 +993,44 @@ Faye.Client = Faye.Class({
995
993
  publication.setDeferredStatus('failed', Faye.Error.parse(response.error));
996
994
  }, this);
997
995
  }, this);
998
-
996
+
999
997
  return publication;
1000
998
  },
1001
-
999
+
1002
1000
  receiveMessage: function(message) {
1003
1001
  this.pipeThroughExtensions('incoming', message, function(message) {
1004
1002
  if (!message) return;
1005
-
1003
+
1006
1004
  if (message.advice) this._handleAdvice(message.advice);
1007
1005
  this._deliverMessage(message);
1008
-
1006
+
1009
1007
  if (message.successful === undefined) return;
1010
-
1008
+
1011
1009
  var callback = this._responseCallbacks[message.id];
1012
1010
  if (!callback) return;
1013
-
1011
+
1014
1012
  delete this._responseCallbacks[message.id];
1015
1013
  callback[0].call(callback[1], message);
1016
1014
  }, this);
1017
1015
  },
1018
-
1016
+
1019
1017
  _selectTransport: function(transportTypes) {
1020
- Faye.Transport.get(this, transportTypes, function(transport) {
1018
+ Faye.Transport.get(this, transportTypes, this._disabled, function(transport) {
1021
1019
  this.debug('Selected ? transport for ?', transport.connectionType, transport.endpoint);
1022
-
1020
+
1021
+ if (transport === this._transport) return;
1022
+ if (this._transport) this._transport.close();
1023
+
1023
1024
  this._transport = transport;
1024
1025
  this._transport.cookies = this._cookies;
1025
1026
  this._transport.headers = this._headers;
1026
-
1027
+
1027
1028
  transport.bind('down', function() {
1028
1029
  if (this._transportUp !== undefined && !this._transportUp) return;
1029
1030
  this._transportUp = false;
1030
1031
  this.trigger('transport:down');
1031
1032
  }, this);
1032
-
1033
+
1033
1034
  transport.bind('up', function() {
1034
1035
  if (this._transportUp !== undefined && this._transportUp) return;
1035
1036
  this._transportUp = true;
@@ -1037,7 +1038,7 @@ Faye.Client = Faye.Class({
1037
1038
  }, this);
1038
1039
  }, this);
1039
1040
  },
1040
-
1041
+
1041
1042
  _send: function(message, callback, context) {
1042
1043
  message.id = this._generateMessageId();
1043
1044
  if (callback) this._responseCallbacks[message.id] = [callback, context];
@@ -1047,7 +1048,7 @@ Faye.Client = Faye.Class({
1047
1048
  this._transport.send(message, this._advice.timeout / 1000);
1048
1049
  }, this);
1049
1050
  },
1050
-
1051
+
1051
1052
  _generateMessageId: function() {
1052
1053
  this._messageId += 1;
1053
1054
  if (this._messageId >= Math.pow(2,32)) this._messageId = 0;
@@ -1056,26 +1057,26 @@ Faye.Client = Faye.Class({
1056
1057
 
1057
1058
  _handleAdvice: function(advice) {
1058
1059
  Faye.extend(this._advice, advice);
1059
-
1060
+
1060
1061
  if (this._advice.reconnect === this.HANDSHAKE && this._state !== this.DISCONNECTED) {
1061
1062
  this._state = this.UNCONNECTED;
1062
1063
  this._clientId = null;
1063
1064
  this._cycleConnection();
1064
1065
  }
1065
1066
  },
1066
-
1067
+
1067
1068
  _deliverMessage: function(message) {
1068
1069
  if (!message.channel || message.data === undefined) return;
1069
1070
  this.info('Client ? calling listeners for ? with ?', this._clientId, message.channel, message.data);
1070
1071
  this._channels.distributeMessage(message);
1071
1072
  },
1072
-
1073
+
1073
1074
  _teardownConnection: function() {
1074
1075
  if (!this._connectRequest) return;
1075
1076
  this._connectRequest = null;
1076
1077
  this.info('Closed connection for ?', this._clientId);
1077
1078
  },
1078
-
1079
+
1079
1080
  _cycleConnection: function() {
1080
1081
  this._teardownConnection();
1081
1082
  var self = this;
@@ -1098,9 +1099,9 @@ Faye.Transport = Faye.extend(Faye.Class({
1098
1099
  this.endpoint = endpoint;
1099
1100
  this._outbox = [];
1100
1101
  },
1101
-
1102
+
1102
1103
  close: function() {},
1103
-
1104
+
1104
1105
  send: function(message, timeout) {
1105
1106
  this.debug('Client ? sending message to ?: ?',
1106
1107
  this._client._clientId, this.endpoint, message);
@@ -1129,48 +1130,50 @@ Faye.Transport = Faye.extend(Faye.Class({
1129
1130
  this._connectMessage.advice = {timeout: 0};
1130
1131
 
1131
1132
  this.request(this._outbox, this._timeout);
1132
-
1133
+
1133
1134
  this._connectMessage = null;
1134
1135
  this._outbox = [];
1135
1136
  },
1136
-
1137
+
1137
1138
  receive: function(responses) {
1138
1139
  this.debug('Client ? received from ?: ?',
1139
1140
  this._client._clientId, this.endpoint, responses);
1140
-
1141
+
1141
1142
  for (var i = 0, n = responses.length; i < n; i++) {
1142
1143
  this._client.receiveMessage(responses[i]);
1143
1144
  }
1144
1145
  },
1145
-
1146
+
1146
1147
  retry: function(message, timeout) {
1147
1148
  var called = false,
1148
1149
  retry = this._client.retry * 1000,
1149
1150
  self = this;
1150
-
1151
+
1151
1152
  return function() {
1152
1153
  if (called) return;
1153
1154
  called = true;
1154
1155
  Faye.ENV.setTimeout(function() { self.request(message, timeout) }, retry);
1155
1156
  };
1156
1157
  }
1157
-
1158
+
1158
1159
  }), {
1159
1160
  MAX_URL_LENGTH: 2048,
1160
-
1161
- get: function(client, connectionTypes, callback, context) {
1161
+
1162
+ get: function(client, allowed, disabled, callback, context) {
1162
1163
  var endpoint = client.endpoint;
1163
- if (connectionTypes === undefined) connectionTypes = this.supportedConnectionTypes();
1164
-
1164
+
1165
1165
  Faye.asyncEach(this._transports, function(pair, resume) {
1166
1166
  var connType = pair[0], klass = pair[1],
1167
1167
  connEndpoint = client.endpoints[connType] || endpoint;
1168
-
1169
- if (Faye.indexOf(connectionTypes, connType) < 0) {
1168
+
1169
+ if (Faye.indexOf(disabled, connType) >= 0)
1170
+ return resume();
1171
+
1172
+ if (Faye.indexOf(allowed, connType) < 0) {
1170
1173
  klass.isUsable(client, connEndpoint, function() {});
1171
1174
  return resume();
1172
1175
  }
1173
-
1176
+
1174
1177
  klass.isUsable(client, connEndpoint, function(isUsable) {
1175
1178
  if (!isUsable) return resume();
1176
1179
  var transport = klass.hasOwnProperty('create') ? klass.create(client, connEndpoint) : new klass(client, connEndpoint);
@@ -1180,17 +1183,13 @@ Faye.Transport = Faye.extend(Faye.Class({
1180
1183
  throw new Error('Could not find a usable connection type for ' + endpoint);
1181
1184
  });
1182
1185
  },
1183
-
1186
+
1184
1187
  register: function(type, klass) {
1185
1188
  this._transports.push([type, klass]);
1186
1189
  klass.prototype.connectionType = type;
1187
1190
  },
1188
-
1189
- _transports: [],
1190
-
1191
- supportedConnectionTypes: function() {
1192
- return Faye.map(this._transports, function(pair) { return pair[0] });
1193
- }
1191
+
1192
+ _transports: []
1194
1193
  });
1195
1194
 
1196
1195
  Faye.extend(Faye.Transport.prototype, Faye.Logging);
@@ -1200,15 +1199,15 @@ Faye.extend(Faye.Transport.prototype, Faye.Timeouts);
1200
1199
 
1201
1200
  Faye.Event = {
1202
1201
  _registry: [],
1203
-
1202
+
1204
1203
  on: function(element, eventName, callback, context) {
1205
1204
  var wrapped = function() { callback.call(context) };
1206
-
1205
+
1207
1206
  if (element.addEventListener)
1208
1207
  element.addEventListener(eventName, wrapped, false);
1209
1208
  else
1210
1209
  element.attachEvent('on' + eventName, wrapped);
1211
-
1210
+
1212
1211
  this._registry.push({
1213
1212
  _element: element,
1214
1213
  _type: eventName,
@@ -1217,23 +1216,23 @@ Faye.Event = {
1217
1216
  _handler: wrapped
1218
1217
  });
1219
1218
  },
1220
-
1219
+
1221
1220
  detach: function(element, eventName, callback, context) {
1222
1221
  var i = this._registry.length, register;
1223
1222
  while (i--) {
1224
1223
  register = this._registry[i];
1225
-
1224
+
1226
1225
  if ((element && element !== register._element) ||
1227
1226
  (eventName && eventName !== register._type) ||
1228
1227
  (callback && callback !== register._callback) ||
1229
1228
  (context && context !== register._context))
1230
1229
  continue;
1231
-
1230
+
1232
1231
  if (register._element.removeEventListener)
1233
1232
  register._element.removeEventListener(register._type, register._handler, false);
1234
1233
  else
1235
1234
  register._element.detachEvent('on' + register._type, register._handler);
1236
-
1235
+
1237
1236
  this._registry.splice(i,1);
1238
1237
  register = null;
1239
1238
  }
@@ -1252,17 +1251,17 @@ Faye.URI = Faye.extend(Faye.Class({
1252
1251
  }
1253
1252
  return pairs.join('&');
1254
1253
  },
1255
-
1254
+
1256
1255
  isSameOrigin: function() {
1257
- var host = Faye.URI.parse(Faye.ENV.location.href);
1258
-
1256
+ var host = Faye.URI.parse(Faye.ENV.location.href, false);
1257
+
1259
1258
  var external = (host.hostname !== this.hostname) ||
1260
1259
  (host.port !== this.port) ||
1261
1260
  (host.protocol !== this.protocol);
1262
-
1261
+
1263
1262
  return !external;
1264
1263
  },
1265
-
1264
+
1266
1265
  toURL: function() {
1267
1266
  var query = this.queryString();
1268
1267
  return this.protocol + '//' + this.hostname + (this.port ? ':' + this.port : '') +
@@ -1272,7 +1271,7 @@ Faye.URI = Faye.extend(Faye.Class({
1272
1271
  parse: function(url, params) {
1273
1272
  if (typeof url !== 'string') return url;
1274
1273
  var uri = new this(), parts;
1275
-
1274
+
1276
1275
  var consume = function(name, pattern, infer) {
1277
1276
  url = url.replace(pattern, function(match) {
1278
1277
  uri[name] = match;
@@ -1281,15 +1280,15 @@ Faye.URI = Faye.extend(Faye.Class({
1281
1280
  if (uri[name] === undefined)
1282
1281
  uri[name] = infer ? Faye.ENV.location[name] : '';
1283
1282
  };
1284
-
1283
+
1285
1284
  consume('protocol', /^https?\:/, true);
1286
1285
  consume('host', /^\/\/[^\/]+/, true);
1287
-
1286
+
1288
1287
  if (!/^\//.test(url)) url = Faye.ENV.location.pathname.replace(/[^\/]*$/, '') + url;
1289
1288
  consume('pathname', /^\/[^\?#]*/);
1290
1289
  consume('search', /^\?[^#]*/);
1291
1290
  consume('hash', /^#.*/);
1292
-
1291
+
1293
1292
  if (/^\/\//.test(uri.host)) {
1294
1293
  uri.host = uri.host.substr(2);
1295
1294
  parts = uri.host.split(':');
@@ -1299,20 +1298,24 @@ Faye.URI = Faye.extend(Faye.Class({
1299
1298
  uri.hostname = Faye.ENV.location.hostname;
1300
1299
  uri.port = Faye.ENV.location.port;
1301
1300
  }
1302
-
1303
- var query = uri.search.replace(/^\?/, ''),
1304
- pairs = query ? query.split('&') : [],
1305
- n = pairs.length,
1306
- data = {};
1307
-
1308
- while (n--) {
1309
- parts = pairs[n].split('=');
1310
- data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
1301
+
1302
+ if (params === false) {
1303
+ uri.params = {};
1304
+ } else {
1305
+ var query = uri.search.replace(/^\?/, ''),
1306
+ pairs = query ? query.split('&') : [],
1307
+ n = pairs.length,
1308
+ data = {};
1309
+
1310
+ while (n--) {
1311
+ parts = pairs[n].split('=');
1312
+ data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
1313
+ }
1314
+ if (typeof params === 'object') Faye.extend(data, params);
1315
+
1316
+ uri.params = data;
1311
1317
  }
1312
- if (typeof params === 'object') Faye.extend(data, params);
1313
-
1314
- uri.params = data;
1315
-
1318
+
1316
1319
  return uri;
1317
1320
  }
1318
1321
  });
@@ -1809,75 +1812,81 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
1809
1812
  CONNECTED: 3,
1810
1813
 
1811
1814
  batching: false,
1812
-
1815
+
1813
1816
  isUsable: function(callback, context) {
1814
1817
  this.callback(function() { callback.call(context, true) });
1815
1818
  this.errback(function() { callback.call(context, false) });
1816
1819
  this.connect();
1817
1820
  },
1818
-
1821
+
1819
1822
  request: function(messages, timeout) {
1820
1823
  if (messages.length === 0) return;
1821
1824
  this._messages = this._messages || {};
1822
-
1825
+
1823
1826
  for (var i = 0, n = messages.length; i < n; i++) {
1824
1827
  this._messages[messages[i].id] = messages[i];
1825
1828
  }
1826
1829
  this.callback(function(socket) { socket.send(Faye.toJSON(messages)) });
1827
1830
  this.connect();
1828
1831
  },
1829
-
1832
+
1830
1833
  close: function() {
1831
- if (this._closed) return;
1832
- this._closed = true;
1833
- if (this._socket) this._socket.close();
1834
+ if (!this._socket) return;
1835
+ this._socket.onclose = this._socket.onerror = null;
1836
+ this._socket.close();
1837
+ delete this._socket;
1838
+ this.setDeferredStatus('deferred');
1839
+ this._state = this.UNCONNECTED;
1834
1840
  },
1835
-
1841
+
1836
1842
  connect: function() {
1837
1843
  if (Faye.Transport.WebSocket._unloaded) return;
1838
- if (this._closed) return;
1839
-
1844
+
1840
1845
  this._state = this._state || this.UNCONNECTED;
1841
1846
  if (this._state !== this.UNCONNECTED) return;
1842
-
1847
+
1843
1848
  this._state = this.CONNECTING;
1844
-
1849
+
1845
1850
  var ws = Faye.Transport.WebSocket.getClass();
1846
1851
  if (!ws) return this.setDeferredStatus('failed');
1847
-
1852
+
1848
1853
  this._socket = new ws(Faye.Transport.WebSocket.getSocketUrl(this.endpoint));
1849
1854
  var self = this;
1850
-
1855
+
1851
1856
  this._socket.onopen = function() {
1852
1857
  self._state = self.CONNECTED;
1853
1858
  self._everConnected = true;
1854
1859
  self.setDeferredStatus('succeeded', self._socket);
1855
1860
  self.trigger('up');
1856
1861
  };
1857
-
1862
+
1858
1863
  this._socket.onmessage = function(event) {
1859
- var messages = [].concat(JSON.parse(event.data));
1864
+ var messages = JSON.parse(event.data);
1865
+ if (!messages) return;
1866
+ messages = [].concat(messages);
1867
+
1860
1868
  for (var i = 0, n = messages.length; i < n; i++) {
1861
1869
  delete self._messages[messages[i].id];
1862
1870
  }
1863
1871
  self.receive(messages);
1864
1872
  };
1865
-
1866
- this._socket.onclose = function() {
1873
+
1874
+ this._socket.onclose = this._socket.onerror = function() {
1867
1875
  var wasConnected = (self._state === self.CONNECTED);
1868
1876
  self.setDeferredStatus('deferred');
1869
1877
  self._state = self.UNCONNECTED;
1870
- delete self._socket;
1871
-
1878
+
1879
+ self.close();
1880
+
1872
1881
  if (wasConnected) return self.resend();
1873
1882
  if (!self._everConnected) return self.setDeferredStatus('failed');
1874
-
1883
+
1875
1884
  var retry = self._client.retry * 1000;
1876
1885
  Faye.ENV.setTimeout(function() { self.connect() }, retry);
1877
1886
  self.trigger('down');
1878
1887
  };
1879
1888
  },
1880
-
1889
+
1881
1890
  resend: function() {
1882
1891
  if (!this._messages) return;
1883
1892
  var messages = Faye.map(this._messages, function(id, msg) { return msg });
@@ -1888,17 +1897,17 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
1888
1897
  if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL();
1889
1898
  return endpoint.replace(/^http(s?):/ig, 'ws$1:');
1890
1899
  },
1891
-
1900
+
1892
1901
  getClass: function() {
1893
1902
  return (Faye.WebSocket && Faye.WebSocket.Client) ||
1894
1903
  Faye.ENV.WebSocket ||
1895
1904
  Faye.ENV.MozWebSocket;
1896
1905
  },
1897
-
1906
+
1898
1907
  isUsable: function(client, endpoint, callback, context) {
1899
1908
  this.create(client, endpoint).isUsable(callback, context);
1900
1909
  },
1901
-
1910
+
1902
1911
  create: function(client, endpoint) {
1903
1912
  var sockets = client.transports.websocket = client.transports.websocket || {};
1904
1913
  sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
@@ -1919,18 +1928,18 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
1919
1928
  initialize: function(client, endpoint) {
1920
1929
  Faye.Transport.prototype.initialize.call(this, client, endpoint);
1921
1930
  if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed');
1922
-
1931
+
1923
1932
  this._xhr = new Faye.Transport.XHR(client, endpoint);
1924
-
1933
+
1925
1934
  var socket = new EventSource(endpoint + '/' + client.getClientId()),
1926
1935
  self = this;
1927
-
1936
+
1928
1937
  socket.onopen = function() {
1929
1938
  self._everConnected = true;
1930
1939
  self.setDeferredStatus('succeeded');
1931
1940
  self.trigger('up');
1932
1941
  };
1933
-
1942
+
1934
1943
  socket.onerror = function() {
1935
1944
  if (self._everConnected) {
1936
1945
  self.trigger('down');
@@ -1939,40 +1948,46 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
1939
1948
  socket.close();
1940
1949
  }
1941
1950
  };
1942
-
1951
+
1943
1952
  socket.onmessage = function(event) {
1944
1953
  self.receive(JSON.parse(event.data));
1945
1954
  self.trigger('up');
1946
1955
  };
1947
-
1956
+
1948
1957
  this._socket = socket;
1949
1958
  },
1950
-
1959
+
1951
1960
  isUsable: function(callback, context) {
1952
1961
  this.callback(function() { callback.call(context, true) });
1953
1962
  this.errback(function() { callback.call(context, false) });
1954
1963
  },
1955
-
1964
+
1956
1965
  request: function(message, timeout) {
1957
1966
  this._xhr.request(message, timeout);
1958
1967
  },
1959
-
1968
+
1960
1969
  close: function() {
1970
+ if (!this._socket) return;
1971
+ this._socket.onerror = null;
1961
1972
  this._socket.close();
1973
+ delete this._socket;
1962
1974
  }
1963
1975
  }), {
1964
1976
  isUsable: function(client, endpoint, callback, context) {
1965
1977
  var id = client.getClientId();
1966
1978
  if (!id) return callback.call(context, false);
1967
-
1979
+
1968
1980
  Faye.Transport.XHR.isUsable(client, endpoint, function(usable) {
1969
1981
  if (!usable) return callback.call(context, false);
1970
1982
  this.create(client, endpoint).isUsable(callback, context);
1971
1983
  }, this);
1972
1984
  },
1973
-
1985
+
1974
1986
  create: function(client, endpoint) {
1975
- var sockets = client.transports.eventsource = client.transports.eventsource || {};
1987
+ var sockets = client.transports.eventsource = client.transports.eventsource || {},
1988
+ id = client.getClientId(),
1989
+ endpoint = endpoint + '/' + (id || '');
1990
+
1976
1991
  sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
1977
1992
  return sockets[endpoint];
1978
1993
  }
@@ -1990,48 +2005,48 @@ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, {
1990
2005
  xhr = Faye.ENV.ActiveXObject
1991
2006
  ? new ActiveXObject("Microsoft.XMLHTTP")
1992
2007
  : new XMLHttpRequest();
1993
-
2008
+
1994
2009
  xhr.open('POST', path, true);
1995
2010
  xhr.setRequestHeader('Content-Type', 'application/json');
1996
2011
  xhr.setRequestHeader('Pragma', 'no-cache');
1997
2012
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
1998
-
2013
+
1999
2014
  var headers = this.headers;
2000
2015
  for (var key in headers) {
2001
2016
  if (!headers.hasOwnProperty(key)) continue;
2002
2017
  xhr.setRequestHeader(key, headers[key]);
2003
2018
  }
2004
-
2019
+
2005
2020
  var abort = function() { xhr.abort() };
2006
2021
  Faye.Event.on(Faye.ENV, 'beforeunload', abort);
2007
-
2022
+
2008
2023
  var cleanUp = function() {
2009
2024
  Faye.Event.detach(Faye.ENV, 'beforeunload', abort);
2010
2025
  xhr.onreadystatechange = function() {};
2011
2026
  xhr = null;
2012
2027
  };
2013
-
2028
+
2014
2029
  xhr.onreadystatechange = function() {
2015
2030
  if (xhr.readyState !== 4) return;
2016
-
2031
+
2017
2032
  var parsedMessage = null,
2018
2033
  status = xhr.status,
2019
2034
  successful = ((status >= 200 && status < 300) ||
2020
2035
  status === 304 ||
2021
2036
  status === 1223);
2022
-
2037
+
2023
2038
  if (!successful) {
2024
2039
  cleanUp();
2025
2040
  retry();
2026
2041
  return self.trigger('down');
2027
2042
  }
2028
-
2043
+
2029
2044
  try {
2030
2045
  parsedMessage = JSON.parse(xhr.responseText);
2031
2046
  } catch (e) {}
2032
-
2047
+
2033
2048
  cleanUp();
2034
-
2049
+
2035
2050
  if (parsedMessage) {
2036
2051
  self.receive(parsedMessage);
2037
2052
  self.trigger('up');
@@ -2057,10 +2072,10 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2057
2072
  xhr = new xhrClass(),
2058
2073
  retry = this.retry(message, timeout),
2059
2074
  self = this;
2060
-
2075
+
2061
2076
  xhr.open('POST', this.endpoint, true);
2062
2077
  if (xhr.setRequestHeader) xhr.setRequestHeader('Pragma', 'no-cache');
2063
-
2078
+
2064
2079
  var cleanUp = function() {
2065
2080
  if (!xhr) return false;
2066
2081
  xhr.onload = xhr.onerror = xhr.ontimeout = xhr.onprogress = null;
@@ -2068,15 +2083,15 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2068
2083
  Faye.ENV.clearTimeout(timer);
2069
2084
  return true;
2070
2085
  };
2071
-
2086
+
2072
2087
  xhr.onload = function() {
2073
2088
  var parsedMessage = null;
2074
2089
  try {
2075
2090
  parsedMessage = JSON.parse(xhr.responseText);
2076
2091
  } catch (e) {}
2077
-
2092
+
2078
2093
  cleanUp();
2079
-
2094
+
2080
2095
  if (parsedMessage) {
2081
2096
  self.receive(parsedMessage);
2082
2097
  self.trigger('up');
@@ -2085,7 +2100,7 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2085
2100
  self.trigger('down');
2086
2101
  }
2087
2102
  };
2088
-
2103
+
2089
2104
  var onerror = function() {
2090
2105
  cleanUp();
2091
2106
  retry();
@@ -2094,7 +2109,7 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2094
2109
  var timer = Faye.ENV.setTimeout(onerror, 1.5 * 1000 * timeout);
2095
2110
  xhr.onerror = onerror;
2096
2111
  xhr.ontimeout = onerror;
2097
-
2112
+
2098
2113
  xhr.onprogress = function() {};
2099
2114
  xhr.send('message=' + encodeURIComponent(Faye.toJSON(message)));
2100
2115
  }
@@ -2102,11 +2117,11 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2102
2117
  isUsable: function(client, endpoint, callback, context) {
2103
2118
  if (Faye.URI.parse(endpoint).isSameOrigin())
2104
2119
  return callback.call(context, false);
2105
-
2120
+
2106
2121
  if (Faye.ENV.XDomainRequest)
2107
2122
  return callback.call(context, Faye.URI.parse(endpoint).protocol ===
2108
2123
  Faye.URI.parse(Faye.ENV.location).protocol);
2109
-
2124
+
2110
2125
  if (Faye.ENV.XMLHttpRequest) {
2111
2126
  var xhr = new Faye.ENV.XMLHttpRequest();
2112
2127
  return callback.call(context, xhr.withCredentials !== undefined);
@@ -2127,7 +2142,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2127
2142
  var location = Faye.URI.parse(this.endpoint, params).toURL();
2128
2143
  return location.length >= Faye.Transport.MAX_URL_LENGTH;
2129
2144
  },
2130
-
2145
+
2131
2146
  request: function(messages, timeout) {
2132
2147
  var params = {message: Faye.toJSON(messages)},
2133
2148
  head = document.getElementsByTagName('head')[0],
@@ -2136,19 +2151,19 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2136
2151
  location = Faye.URI.parse(this.endpoint, params),
2137
2152
  retry = this.retry(messages, timeout),
2138
2153
  self = this;
2139
-
2154
+
2140
2155
  Faye.ENV[callbackName] = function(data) {
2141
2156
  cleanUp();
2142
2157
  self.receive(data);
2143
2158
  self.trigger('up');
2144
2159
  };
2145
-
2160
+
2146
2161
  var timer = Faye.ENV.setTimeout(function() {
2147
2162
  cleanUp();
2148
2163
  retry();
2149
2164
  self.trigger('down');
2150
2165
  }, 1.5 * 1000 * timeout);
2151
-
2166
+
2152
2167
  var cleanUp = function() {
2153
2168
  if (!Faye.ENV[callbackName]) return false;
2154
2169
  Faye.ENV[callbackName] = undefined;
@@ -2157,7 +2172,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2157
2172
  script.parentNode.removeChild(script);
2158
2173
  return true;
2159
2174
  };
2160
-
2175
+
2161
2176
  location.params.jsonp = callbackName;
2162
2177
  script.type = 'text/javascript';
2163
2178
  script.src = location.toURL();
@@ -2165,12 +2180,12 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2165
2180
  }
2166
2181
  }), {
2167
2182
  _cbCount: 0,
2168
-
2183
+
2169
2184
  getCallbackName: function() {
2170
2185
  this._cbCount += 1;
2171
2186
  return '__jsonp' + this._cbCount + '__';
2172
2187
  },
2173
-
2188
+
2174
2189
  isUsable: function(client, endpoint, callback, context) {
2175
2190
  callback.call(context, true);
2176
2191
  }