@emartech/faye-redis-sharded
Version:
Redis backend engine for Faye with support for sharding
81 lines (63 loc) • 2.35 kB
JavaScript
Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
initialize: function(client, endpoint) {
Faye.Transport.prototype.initialize.call(this, client, endpoint);
if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed');
this._xhr = new Faye.Transport.XHR(client, endpoint);
endpoint = Faye.copyObject(endpoint);
endpoint.pathname += '/' + client._clientId;
var socket = new EventSource(Faye.URI.stringify(endpoint)),
self = this;
socket.onopen = function() {
self._everConnected = true;
self.setDeferredStatus('succeeded');
};
socket.onerror = function() {
if (self._everConnected) {
self._client.messageError([]);
} else {
self.setDeferredStatus('failed');
socket.close();
}
};
socket.onmessage = function(event) {
self.receive([], JSON.parse(event.data));
};
this._socket = socket;
},
close: function() {
if (!this._socket) return;
this._socket.onopen = this._socket.onerror = this._socket.onmessage = null;
this._socket.close();
delete this._socket;
},
isUsable: function(callback, context) {
this.callback(function() { callback.call(context, true) });
this.errback(function() { callback.call(context, false) });
},
encode: function(envelopes) {
return this._xhr.encode(envelopes);
},
request: function(envelopes) {
this._xhr.request(envelopes);
}
}), {
isUsable: function(client, endpoint, callback, context) {
var id = client._clientId;
if (!id) return callback.call(context, false);
Faye.Transport.XHR.isUsable(client, endpoint, function(usable) {
if (!usable) return callback.call(context, false);
this.create(client, endpoint).isUsable(callback, context);
}, this);
},
create: function(client, endpoint) {
var sockets = client.transports.eventsource = client.transports.eventsource || {},
id = client._clientId;
endpoint = Faye.copyObject(endpoint);
endpoint.pathname += '/' + (id || '');
var url = Faye.URI.stringify(endpoint);
sockets[url] = sockets[url] || new this(client, endpoint);
return sockets[url];
}
});
Faye.extend(Faye.Transport.EventSource.prototype, Faye.Deferrable);
Faye.Transport.register('eventsource', Faye.Transport.EventSource);