Skip to content

Commit

Permalink
Set EventSource readyState during constructor and attach pings and ev…
Browse files Browse the repository at this point in the history
…ent listeners after sending handshake.
  • Loading branch information
jcoglan committed Feb 13, 2012
1 parent 74b76e5 commit 8f71ec1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/faye/eventsource.js
Expand Up @@ -28,16 +28,10 @@ var EventSource = function(request, response, options) {
this.lastEventId = request.headers['last-event-id'] || '';

var self = this;
this.readyState = API.CONNECTING;
this._sendBuffer = [];
process.nextTick(function() { self._open() });

if (this._ping)
this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000);

['close', 'end', 'error'].forEach(function(event) {
self._stream.addListener(event, function() { self.close() });
});

var handshake = 'HTTP/1.1 200 OK\r\n' +
'Content-Type: text/event-stream\r\n' +
'Cache-Control: no-cache, no-store\r\n' +
Expand All @@ -47,6 +41,15 @@ var EventSource = function(request, response, options) {
try {
this._stream.write(handshake, 'utf8');
} catch (e) {}

this.readyState = API.OPEN;

if (this._ping)
this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000);

['close', 'end', 'error'].forEach(function(event) {
self._stream.addListener(event, function() { self.close() });
});
};

EventSource.isEventSource = function(request) {
Expand Down

0 comments on commit 8f71ec1

Please sign in to comment.