From 83a820b720cb6659dcd2e11ea1103459563839d8 Mon Sep 17 00:00:00 2001 From: John McLear Date: Tue, 12 Mar 2013 16:59:15 +0000 Subject: [PATCH 1/3] new function for handling custom messages, allows objects to be sent, before we only allowed strings --- src/node/handler/PadMessageHandler.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index c046f130..076f31a5 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -254,6 +254,23 @@ function handleSaveRevisionMessage(client, message){ }); } +/** + * Handles a custom message, different to the function below as it handles objects not strings and you can direct the message to specific sessionID + * + * @param msg {Object} the message we're sending + * @param sessionID {string} the socketIO session to which we're sending this message + */ +exports.handleCustomObjectMessage = function (msg, sessionID, cb) { + if(sessionID){ // If a sessionID is targeted then send directly to this sessionID + io.sockets.socket(sessionID).emit(msg); // send a targeted message + }else{ + socketio.sockets.in(msg.data.padId).json.send(msg); // broadcast to all clients on this pad + } + + cb(null, {}); +} + + /** * Handles a custom message (sent via HTTP API request) * From 13ad46aa67cf87d2c5ee86372a101d44f4de7b65 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 16 Mar 2013 13:19:12 +0000 Subject: [PATCH 2/3] a safer approach I think but still be careful --- src/node/handler/PadMessageHandler.js | 11 ++++++----- src/static/js/collab_client.js | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index e44ced05..b254053d 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -261,12 +261,13 @@ function handleSaveRevisionMessage(client, message){ * @param sessionID {string} the socketIO session to which we're sending this message */ exports.handleCustomObjectMessage = function (msg, sessionID, cb) { - if(sessionID){ // If a sessionID is targeted then send directly to this sessionID - io.sockets.socket(sessionID).emit(msg); // send a targeted message - }else{ - socketio.sockets.in(msg.data.padId).json.send(msg); // broadcast to all clients on this pad + if(msg.type === "CUSTOM"){ + if(sessionID){ // If a sessionID is targeted then send directly to this sessionID + io.sockets.socket(sessionID).emit(msg); // send a targeted message + }else{ + socketio.sockets.in(msg.data.padId).json.send(msg); // broadcast to all clients on this pad + } } - cb(null, {}); } diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.js index 94149123..ff360419 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.js @@ -278,8 +278,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) if (!getSocket()) return; if (!evt.data) return; var wrapper = evt; - if (wrapper.type != "COLLABROOM") return; + if (wrapper.type != "COLLABROOM" && wrapper.type != "CUSTOM") return; var msg = wrapper.data; + if (msg.type == "NEW_CHANGES") { var newRev = msg.newRev; @@ -390,6 +391,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) callbacks.onUserLeave(userInfo); } } + else if (msg.type == "DISCONNECT_REASON") { appLevelDisconnectReason = msg.reason; From 2916b39c24f469c50e090b1cb1462e9a9d384e5b Mon Sep 17 00:00:00 2001 From: John McLear Date: Tue, 19 Mar 2013 16:21:04 +0000 Subject: [PATCH 3/3] make sure the sessionID target is right --- src/node/handler/PadMessageHandler.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index bcc9023d..4b7abb6b 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -156,7 +156,6 @@ exports.handleMessage = function(client, message) // handleMessage will be called, even if the client is not authorized hooks.aCallAll("handleMessage", { client: client, message: message }, function ( err, messages ) { if(ERR(err, callback)) return; - _.each(messages, function(newMessage){ if ( newMessage === null ) { dropMessage = true; @@ -193,6 +192,7 @@ exports.handleMessage = function(client, message) handleSuggestUserName(client, message); } else { messageLogger.warn("Dropped message, unknown COLLABROOM Data Type " + message.data.type); +console.warn(message); } } else { messageLogger.warn("Dropped message, unknown Message Type " + message.type); @@ -261,9 +261,12 @@ function handleSaveRevisionMessage(client, message){ * @param sessionID {string} the socketIO session to which we're sending this message */ exports.handleCustomObjectMessage = function (msg, sessionID, cb) { - if(msg.type === "CUSTOM"){ + if(msg.data.type === "CUSTOM"){ if(sessionID){ // If a sessionID is targeted then send directly to this sessionID - io.sockets.socket(sessionID).emit(msg); // send a targeted message + console.warn("Sent msg", msg); + console.warn("to sessionID", sessionID); + // socketio.clients[sessionID].send(msg); + socketio.sockets.socket(sessionID).emit(msg); // send a targeted message }else{ socketio.sockets.in(msg.data.padId).json.send(msg); // broadcast to all clients on this pad } @@ -1496,3 +1499,5 @@ exports.padUsers = function (padID, callback) { callback(null, {padUsers: result}); }); } + +exports.sessioninfos = sessioninfos;