[Groonga-commit] droonga/express-droonga at 910b83b [master] Add "HTTPStreaming" type command

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Jan 6 14:55:10 JST 2014


YUKI Hiroshi	2014-01-06 14:55:10 +0900 (Mon, 06 Jan 2014)

  New Revision: 910b83ba47e30f137c5a52776dd2827e3c9f9f27
  https://github.com/droonga/express-droonga/commit/910b83ba47e30f137c5a52776dd2827e3c9f9f27

  Message:
    Add "HTTPStreaming" type command

  Modified files:
    lib/adapter/command.js

  Modified: lib/adapter/command.js (+58 -0)
===================================================================
--- lib/adapter/command.js    2014-01-06 12:35:31 +0900 (dd176c0)
+++ lib/adapter/command.js    2014-01-06 14:55:10 +0900 (db278aa)
@@ -116,6 +116,64 @@ exports.HTTPRequestResponse = HTTPRequestResponse;
 
 
 
+function HTTPStreaming(options) {
+  Command.apply(this, arguments);
+}
+HTTPStreaming.extend = function(targetClass) {
+  HTTPCommand.extend(targetClass);
+  if (targetClass.commandTypes.indexOf(this) > -1)
+    return;
+  targetClass.commandTypes.push(this);
+
+  Object.defineProperty(targetClass.prototype, 'subscription', {
+    get: function() { return this._options.subscription; }
+  });
+  Object.defineProperty(targetClass.prototype, 'unsubscription', {
+    get: function() { return this._options.unsubscription; }
+  });
+  Object.defineProperty(targetClass.prototype, 'notification', {
+    get: function() { return this._options.notification; }
+  });
+  Object.defineProperty(targetClass.prototype, 'createSubscription', {
+    get: function() { return this._options.createSubscription; }
+  });
+
+  var connectionCount = 0;
+  targetClass.prototype.onHandle = function(request, response, connection) {
+    connectionCount++;
+
+    response.writeHead(200, {
+      'Content-Type':      'application/json',
+      'Transfer-Encoding': 'chunked'
+    });
+
+    connection.on(this.notification, function(data) {
+      response.write(JSON.stringify(data));
+    });
+
+    var subscriptionMessage = this.createSubscription(request);
+    if (!subscriptionMessage.route) {
+      var route = connection.routeToSelf;
+      subscriptionMessage.route = route;
+    }
+    if (!subscriptionMessage.subscriberID) {
+      var subscriberID = sha1sum(route + '#type=' + this.notification + '&id=' + connectionsCount);
+      subscriptionMessage.subscriberID = subscriberID;
+    }
+    connection.emit(this.subscription, subscriptionMessage);
+
+// XXX How we handle the disconnection?
+//    var self = this;
+//    request.on('???', function() {
+//      connection.emit(self.unsubscription, subscriptionMessage);
+//    });
+  };
+};
+HTTPStreaming.extend(HTTPStreaming);
+exports.HTTPStreaming = HTTPStreaming;
+
+
+
 function SocketCommand() {
   Command.apply(this, arguments);
 }
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index