YUKI Hiroshi
null+****@clear*****
Mon Jan 6 18:44:12 JST 2014
YUKI Hiroshi 2014-01-06 18:44:12 +0900 (Mon, 06 Jan 2014) New Revision: e14a6a7e0b524ecb9ef28654f73c30dac9e5a94e https://github.com/droonga/express-droonga/commit/e14a6a7e0b524ecb9ef28654f73c30dac9e5a94e Message: Handle disconnection for HTTP streaming commands Modified files: lib/adapter/command.js Modified: lib/adapter/command.js (+34 -10) =================================================================== --- lib/adapter/command.js 2014-01-06 18:24:06 +0900 (43feae3) +++ lib/adapter/command.js 2014-01-06 18:44:12 +0900 (0f2dded) @@ -139,6 +139,11 @@ HTTPStreaming.extend = function(targetClass) { }); if (!targetClass.defaultDelimiter) targetClass.defaultDelimiter = '\n'; + Object.defineProperty(targetClass.prototype, 'lifetime', { + get: function() { return this._options.lifetime || targetClass.defaultLifetime; } + }); + if (!targetClass.defaultLifetime) + targetClass.defaultLifetime = 10 * 60 * 1000; // 10 min Object.defineProperty(targetClass.prototype, 'createSubscription', { get: function() { return this._options.createSubscription; } @@ -147,16 +152,17 @@ HTTPStreaming.extend = function(targetClass) { var connectionsCount = 0; targetClass.prototype.onHandle = function(request, response, connection) { connectionsCount++; + var self = this; response.writeHead(200, { 'Content-Type': 'application/jsons', 'Transfer-Encoding': 'chunked' }); - var delimiter = this.delimiter; - connection.on(this.notification, function(data) { - response.write(JSON.stringify(data) + delimiter); - }); + var onNotify = function(data) { + response.write(JSON.stringify(data) + self.delimiter); + }; + connection.on(this.notification, onNotify); var subscriptionMessage = this.createSubscription(request); if (!subscriptionMessage.route) { @@ -167,13 +173,31 @@ HTTPStreaming.extend = function(targetClass) { var subscriberID = sha1sum(route + '#type=' + this.notification + '&id=' + connectionsCount); subscriptionMessage.subscriber = subscriberID; } - connection.emit(this.subscription, subscriptionMessage); -// XXX How we handle the disconnection? -// var self = this; -// request.on('???', function() { -// connection.emit(self.unsubscription, subscriptionMessage); -// }); + var subscribe = function() { + connection.emit(self.subscription, subscriptionMessage); + }; + + var live = true; + var onDisconnected = function() { + if (!live) + return; + live = false; + connection.emit(self.unsubscription, subscriptionMessage); + request.removeListener('close', onDisconnected); + response.removeListener('close', onDisconnected); + connection.removeListener(self.notification, onNotify); + }; + request.on('close', onDisconnected); + response.on('close', onDisconnected); + + subscribe(); + var reSubscribeTimer = setInterval(function() { + if (live) + subscribe(); + else + clearInterval(reSubscribeTimer); + }, this.lifetime * 0.5); }; }; HTTPStreaming.extend(HTTPStreaming); -------------- next part -------------- HTML����������������������������...Download