Send messages JSON-formatted to support a wider set of types.

This commit is contained in:
Ian Gulliver
2014-05-13 17:48:30 +03:00
parent e7221f9050
commit cf442ed834

View File

@@ -97,13 +97,13 @@ Cosmopolite.prototype.unsubscribe = function(subject) {
* Post a message to the given subject, storing it and notifying all listeners. * Post a message to the given subject, storing it and notifying all listeners.
* *
* @param {!string} subject Subject name * @param {!string} subject Subject name
* @param {!string} message Message string * @param {!*} message Message string or object
* @param {string=} key Key name to associate this message with * @param {string=} key Key name to associate this message with
*/ */
Cosmopolite.prototype.sendMessage = function(subject, message, key) { Cosmopolite.prototype.sendMessage = function(subject, message, key) {
args = { args = {
'subject': subject, 'subject': subject,
'message': message, 'message': JSON.stringify(message),
}; };
if (key) { if (key) {
args['key'] = key; args['key'] = key;
@@ -357,11 +357,11 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
} }
break; break;
case 'message': case 'message':
if (!(e['subject'] in this.subscriptions_)) { var subscription = this.subscriptions_[e['subject']];
if (!subscription) {
console.log('Message from unrecognized subject:', e); console.log('Message from unrecognized subject:', e);
break; break;
} }
var subscription = this.subscriptions_[e['subject']];
var duplicate = subscription.messages.some(function(message) { var duplicate = subscription.messages.some(function(message) {
return message['id'] == e.id; return message['id'] == e.id;
}); });
@@ -369,6 +369,7 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
console.log('Duplicate message:', e); console.log('Duplicate message:', e);
break; break;
} }
e['message'] = JSON.parse(e['message']);
subscription.messages.push(e); subscription.messages.push(e);
if ('onMessage' in this.callbacks_) { if ('onMessage' in this.callbacks_) {
this.callbacks_['onMessage'](e); this.callbacks_['onMessage'](e);