Store output socket as an instance variable in EventSource.

This commit is contained in:
James Coglan
2012-01-12 19:53:06 +00:00
parent 20dd83257a
commit 91eb2642ee
+5 -4
View File
@@ -15,6 +15,7 @@ var EventSource = function(request, response, options) {
this._request = request;
this._response = response;
this._stream = response.socket;
this._ping = options.ping || this.DEFAULT_PING;
this._retry = options.retry || this.DEFAULT_RETRY;
@@ -30,11 +31,11 @@ var EventSource = function(request, response, options) {
var self = this;
this._pingLoop = setInterval(function() {
try { this._response.socket.write(':\r\n\r\n') } catch (e) {}
try { this._stream.write(':\r\n\r\n') } catch (e) {}
}, this._ping * 1000);
['close', 'end', 'error'].forEach(function(event) {
self._response.socket.addListener(event, function() { self.close() });
self._stream.addListener(event, function() { self.close() });
});
var handshake = 'HTTP/1.1 200 OK\r\n' +
@@ -44,7 +45,7 @@ var EventSource = function(request, response, options) {
'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';
try {
response.socket.write(handshake, 'utf8');
this._stream.write(handshake, 'utf8');
} catch (e) {}
};
@@ -67,7 +68,7 @@ var instance = {
frame += 'data: ' + message + '\r\n\r\n';
try {
this._response.socket.write(frame, 'utf8');
this._stream.write(frame, 'utf8');
} catch (e) {}
},