From cf442ed834c291ca9483efe86669eddb8f1788bb Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 13 May 2014 17:48:30 +0300 Subject: [PATCH] Send messages JSON-formatted to support a wider set of types. --- static/cosmopolite.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index a51fef4..349a5f5 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -97,13 +97,13 @@ Cosmopolite.prototype.unsubscribe = function(subject) { * Post a message to the given subject, storing it and notifying all listeners. * * @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 */ Cosmopolite.prototype.sendMessage = function(subject, message, key) { args = { 'subject': subject, - 'message': message, + 'message': JSON.stringify(message), }; if (key) { args['key'] = key; @@ -357,11 +357,11 @@ Cosmopolite.prototype.onServerEvent_ = function(e) { } break; case 'message': - if (!(e['subject'] in this.subscriptions_)) { + var subscription = this.subscriptions_[e['subject']]; + if (!subscription) { console.log('Message from unrecognized subject:', e); break; } - var subscription = this.subscriptions_[e['subject']]; var duplicate = subscription.messages.some(function(message) { return message['id'] == e.id; }); @@ -369,6 +369,7 @@ Cosmopolite.prototype.onServerEvent_ = function(e) { console.log('Duplicate message:', e); break; } + e['message'] = JSON.parse(e['message']); subscription.messages.push(e); if ('onMessage' in this.callbacks_) { this.callbacks_['onMessage'](e);