-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented publish / subscribe messages.
- Loading branch information
Tiago Alves
committed
Oct 16, 2014
1 parent
f5448be
commit ecef57c
Showing
7 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var messageTypes = require('../utils/messageTypes'); | ||
var PubSubMessage = require('../models/PubSubMessage'); | ||
|
||
// | ||
// publishMessage middleware | ||
// Intercepts custom publish messages | ||
// | ||
// Emits events: | ||
// - message: function (type, data[, callback]) | ||
// - callback: function (success[, message, data]). | ||
// Only if the message is an RPC request. | ||
function publishMessage(muzzData, next){ | ||
var _this = this; | ||
|
||
muzzData = muzzData || {}; | ||
if (!muzzData.h || !muzzData.h.t || !muzzData.d) { | ||
return next(muzzData); | ||
} | ||
|
||
if (muzzData.a === 'publish') { | ||
var msgType = muzzData.h.t; | ||
|
||
var channel = _this.channelManager.get(muzzData.h.ch); | ||
|
||
var pubSubMessage = new PubSubMessage({ raw: muzzData }); | ||
|
||
if (channel) { | ||
switch (msgType) { | ||
case messageTypes.MESSAGE_TYPE_REQUEST: | ||
// RPC message, requires a response | ||
channel.trigger('message', pubSubMessage, function (success, message, data) { | ||
|
||
if (typeof success === 'boolean') { | ||
|
||
if (typeof message === 'object' && arguments.length === 2) { | ||
// (success, data) provided | ||
data = message; | ||
message = ''; | ||
} | ||
|
||
} else if (typeof success === 'string') { | ||
// (message, data) provided | ||
data = message; | ||
message = success; | ||
success = true; | ||
} else if (typeof success === 'object') { | ||
// (data) provided | ||
data = success; | ||
message = ''; | ||
success = true; | ||
} else { | ||
// No arguments provided | ||
data = {}; | ||
message = ''; | ||
success = true; | ||
} | ||
|
||
_this.remoteCalls.response(muzzData.h, success, message, data); | ||
}); | ||
return; | ||
case messageTypes.MESSAGE_TYPE_SIGNAL: | ||
channel.trigger('message', pubSubMessage); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
return next(muzzData); | ||
} | ||
|
||
module.exports = publishMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var PubSubMessage = function (options) { | ||
this.raw = options.raw; | ||
}; | ||
|
||
PubSubMessage.prototype.getRawMessage = function () { | ||
return this.raw; | ||
}; | ||
|
||
PubSubMessage.prototype.getRawMessageData = function () { | ||
return this.getRawMessage().d; | ||
}; | ||
|
||
PubSubMessage.prototype.getNamespace = function () { | ||
return this.getRawMessageData().ns; | ||
}; | ||
|
||
PubSubMessage.prototype.getPayload = function () { | ||
return this.getRawMessageData().p; | ||
}; | ||
|
||
/** | ||
* Get the information of the user that published the message. | ||
* This info is only available for messages published by users | ||
* and not apps. | ||
* | ||
* @return {object} The user info. | ||
*/ | ||
PubSubMessage.prototype.getUser = function () { | ||
return this.getRawMessageData().u; | ||
}; | ||
|
||
exports = module.exports = PubSubMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters