From 8bc0de81bddf9b7d51ac8a1391bccb6af9b90f74 Mon Sep 17 00:00:00 2001 From: YUKI Hiroshi Date: Thu, 1 Aug 2013 15:38:02 +0900 Subject: [PATCH] Call "onSubscribed" handler for pubsub commands --- lib/adapter/command.js | 3 +++ lib/adapter/socket.io.js | 27 +++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/adapter/command.js b/lib/adapter/command.js index 628bbf3..506ffaf 100644 --- a/lib/adapter/command.js +++ b/lib/adapter/command.js @@ -44,6 +44,9 @@ util.inherits(PublishSubscribe, Command); Object.defineProperty(PublishSubscribe.prototype, 'onSubscribe', { get: function() { return this._options.onSubscribe; } }); +Object.defineProperty(PublishSubscribe.prototype, 'onSubscribed', { + get: function() { return this._options.onSubscribed; } +}); Object.defineProperty(PublishSubscribe.prototype, 'onPublish', { get: function() { return this._options.onPublish; } }); diff --git a/lib/adapter/socket.io.js b/lib/adapter/socket.io.js index 1517d81..445d9aa 100644 --- a/lib/adapter/socket.io.js +++ b/lib/adapter/socket.io.js @@ -43,10 +43,17 @@ exports.register = function(application, server, params) { var event = commandName; - var callback = null; + var requestHandler = 'onRequest'; + var responseHandler = 'onResponse'; + + if (command.PublishSubscribe.isInstance(commandDefinition)) { + event += '.subscribe'; + requestHandler = 'onSubscribe'; + responseHandler = 'onSubscribed'; + } + var options = {}; - if (command.RequestResponse.isInstance(commandDefinition)) { - callback = function(error, envelope) { + var callback = function(error, envelope) { if (error) { socket.emit('error', error); return; @@ -59,9 +66,9 @@ exports.register = function(application, server, params) { if (clientOptions.responseEvent) responseEvent = clientOptions.responseEvent; - if (commandDefinition.onResponse) { + if (commandDefinition[responseHandler]) { try { - commandDefinition.onResponse(responseData, wrappedSocket); + commandDefinition[responseHandler](responseData, wrappedSocket); } catch(error) { wrappedSocket.emit('error', error.message || error); } @@ -70,17 +77,13 @@ exports.register = function(application, server, params) { wrappedSocket.destroy(); } }; - options.timeout = DEFAULT_TIMEOUT; - } else if (command.PublishSubscribe.isInstance(commandDefinition)) { - event += '.subscribe'; - } - + options.timeout = DEFAULT_TIMEOUT; options.sessionId = socket.id; var wrappedConection = new wrapper.DroongaProtocolConnectionWrapper(connection, callback, options); - if (commandDefinition.onRequest) { + if (commandDefinition[requestHandler]) { try { - commandDefinition.onRequest(data, wrappedConection); + commandDefinition[requestHandler](data, wrappedConection); } catch(error) { wrappedConection.destroy(); socket.emit('error', error.message || error);