From f28a5c18c656715fb503cbff0caf1cbd058d671e Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Sun, 27 Mar 2011 12:49:04 +0100 Subject: [PATCH] Some improvements on message handling, on client and serverside --- node/MessageHandler.js | 13 +++++++++++++ static/js/collab_client.js | 8 ++++++-- static/js/pad2.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/node/MessageHandler.js b/node/MessageHandler.js index b0991a90..4f15725c 100644 --- a/node/MessageHandler.js +++ b/node/MessageHandler.js @@ -346,6 +346,19 @@ function handleClientReady(client, message) //Ask the author Manager for a authorname of this token. var author = authorManager.getAuthor4Token(message.token); + //Check if this author is already on the pad, if yes, kick him! + if(pad2sessions[message.padId]) + { + for(var i in pad2sessions[message.padId]) + { + if(sessioninfos[pad2sessions[message.padId][i]].author == author) + { + client.send({disconnect:"doublelogin"}); + return; + } + } + } + //Save in session2pad that this session belonges to this pad var sessionId=String(client.sessionId); session2pad[sessionId] = message.padId; diff --git a/static/js/collab_client.js b/static/js/collab_client.js index be38eae0..5606d656 100644 --- a/static/js/collab_client.js +++ b/static/js/collab_client.js @@ -195,11 +195,11 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) { initialStartConnectTime = +new Date(); // }); - socket.on('message', function(obj){ + /*socket.on('message', function(obj){ if(window.console) console.log(obj); handleMessageFromServer(obj); - }); + });*/ socket.on('disconnect', function(obj){ handleSocketClosed(true); @@ -297,6 +297,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) { } function handleMessageFromServer(evt) { + if(window.console) + console.log(evt); + if (! socket) return; if (! evt.data) return; var wrapper = evt; @@ -636,6 +639,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) { setOnConnectionTrouble: function(cb) { callbacks.onConnectionTrouble = cb; }, setOnServerMessage: function(cb) { callbacks.onServerMessage = cb; }, updateUserInfo: defer(updateUserInfo), + handleMessageFromServer: handleMessageFromServer, getConnectedUsers: getConnectedUsers, sendClientMessage: sendClientMessage, getCurrentRevisionNumber: getCurrentRevisionNumber, diff --git a/static/js/pad2.js b/static/js/pad2.js index 7e472983..71e3ad1e 100644 --- a/static/js/pad2.js +++ b/static/js/pad2.js @@ -85,23 +85,42 @@ function handshake() var initalized = false; socket.on('message', function(obj){ + //if we haven't recieved the clientVars yet, then this message should it be if(!receivedClientVars) { - receivedClientVars=true; + //We get a disconnect message + if(obj.disconnect) + { + socket.disconnect(); + alert("You have this Pad already opened in another Window/Tab"); + return; + } + //yeah, the clientVars are here :). So we can start initalizing the Pad + else + { + receivedClientVars=true; - clientVars = obj; - clientVars.userAgent=navigator.userAgent; - clientVars.collab_client_vars.clientAgent=navigator.userAgent; - - pad.init(); - - initalized=true; + clientVars = obj; + clientVars.userAgent=navigator.userAgent; + clientVars.collab_client_vars.clientAgent=navigator.userAgent; + + pad.init(); + + initalized=true; + } } + //This handles every Message after the clientVars else { + //We can't handle the message before we initalized the pad, so let this message bounce back in 100ms if(!initalized) { - setTimeOut(this(obj)); + setTimeOut(this(obj), 100); + } + //We're initalized, so give this message to the collabClient + else + { + pad.collabClient.handleMessageFromServer(obj); } } });