Merge branch 'switch-to-pad' of https://github.com/derosm2/etherpad-lite into switch-to-pad

switch-to-pad
John McLear 2014-11-15 16:33:23 +00:00
commit 4297fd5916
2 changed files with 110 additions and 53 deletions

View File

@ -223,6 +223,8 @@ exports.handleMessage = function(client, message)
} else { } else {
messageLogger.warn("Dropped message, unknown COLLABROOM Data Type " + message.data.type); messageLogger.warn("Dropped message, unknown COLLABROOM Data Type " + message.data.type);
} }
} else if(message.type == "SWITCH_TO_PAD") {
handleSwitchToPad(client, message);
} else { } else {
messageLogger.warn("Dropped message, unknown Message Type " + message.type); messageLogger.warn("Dropped message, unknown Message Type " + message.type);
} }
@ -236,18 +238,7 @@ exports.handleMessage = function(client, message)
{ {
// client tried to auth for the first time (first msg from the client) // client tried to auth for the first time (first msg from the client)
if(message.type == "CLIENT_READY") { if(message.type == "CLIENT_READY") {
// Remember this information since we won't createSessionInfo(client, message);
// have the cookie in further socket.io messages.
// This information will be used to check if
// the sessionId of this connection is still valid
// since it could have been deleted by the API.
sessioninfos[client.id].auth =
{
sessionID: message.sessionID,
padID: message.padId,
token : message.token,
password: message.password
};
} }
// Note: message.sessionID is an entirely different kind of // Note: message.sessionID is an entirely different kind of
@ -882,6 +873,42 @@ function _correctMarkersInPad(atext, apool) {
return builder.toString(); return builder.toString();
} }
function handleSwitchToPad(client, message)
{
// clear the session and leave the room
var currentSession = sessioninfos[client.id];
var padId = currentSession.padId;
var roomClients = socketio.sockets.clients(padId);
for(var i = 0; i < roomClients.length; i++) {
var sinfo = sessioninfos[roomClients[i].id];
if(sinfo && sinfo.author == currentSession.author) {
// fix user's counter, works on page refresh or if user closes browser window and then rejoins
sessioninfos[roomClients[i].id] = {};
roomClients[i].leave(padId);
}
}
// start up the new pad
createSessionInfo(client, message);
handleClientReady(client, message);
}
function createSessionInfo(client, message)
{
// Remember this information since we won't
// have the cookie in further socket.io messages.
// This information will be used to check if
// the sessionId of this connection is still valid
// since it could have been deleted by the API.
sessioninfos[client.id].auth =
{
sessionID: message.sessionID,
padID: message.padId,
token : message.token,
password: message.password
};
}
/** /**
* Handles a CLIENT_READY. A CLIENT_READY is the first message from the client to the server. The Client sends his token * Handles a CLIENT_READY. A CLIENT_READY is the first message from the client to the server. The Client sends his token
* and the pad it wants to enter. The Server answers with the inital values (clientVars) of the pad * and the pad it wants to enter. The Server answers with the inital values (clientVars) of the pad

View File

@ -51,6 +51,8 @@ var gritter = require('./gritter').gritter;
var hooks = require('./pluginfw/hooks'); var hooks = require('./pluginfw/hooks');
var receivedClientVars = false;
function createCookie(name, value, days, path){ /* Warning Internet Explorer doesn't use this it uses the one from pad_utils.js */ function createCookie(name, value, days, path){ /* Warning Internet Explorer doesn't use this it uses the one from pad_utils.js */
if (days) if (days)
{ {
@ -160,6 +162,49 @@ function savePassword()
return false; return false;
} }
function sendClientReady(isReconnect, messageType)
{
messageType = typeof messageType !== 'undefined' ? messageType : 'CLIENT_READY';
var padId = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
padId = decodeURIComponent(padId); // unescape neccesary due to Safari and Opera interpretation of spaces
if(!isReconnect)
{
var titleArray = document.title.split('|');
var title = titleArray[titleArray.length - 1];
document.title = padId.replace(/_+/g, ' ') + " | " + title;
}
var token = readCookie("token");
if (token == null)
{
token = "t." + randomString();
createCookie("token", token, 60);
}
var sessionID = decodeURIComponent(readCookie("sessionID"));
var password = readCookie("password");
var msg = {
"component": "pad",
"type": messageType,
"padId": padId,
"sessionID": sessionID,
"password": password,
"token": token,
"protocolVersion": 2
};
//this is a reconnect, lets tell the server our revisionnumber
if(isReconnect == true)
{
msg.client_rev=pad.collabClient.getCurrentRevisionNumber();
msg.reconnect=true;
}
socket.json.send(msg);
}
function handshake() function handshake()
{ {
var loc = document.location; var loc = document.location;
@ -176,44 +221,6 @@ function handshake()
'sync disconnect on unload' : false 'sync disconnect on unload' : false
}); });
function sendClientReady(isReconnect)
{
var padId = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
padId = decodeURIComponent(padId); // unescape neccesary due to Safari and Opera interpretation of spaces
if(!isReconnect)
document.title = padId.replace(/_+/g, ' ') + " | " + document.title;
var token = readCookie("token");
if (token == null)
{
token = "t." + randomString();
createCookie("token", token, 60);
}
var sessionID = decodeURIComponent(readCookie("sessionID"));
var password = readCookie("password");
var msg = {
"component": "pad",
"type": "CLIENT_READY",
"padId": padId,
"sessionID": sessionID,
"password": password,
"token": token,
"protocolVersion": 2
};
//this is a reconnect, lets tell the server our revisionnumber
if(isReconnect == true)
{
msg.client_rev=pad.collabClient.getCurrentRevisionNumber();
msg.reconnect=true;
}
socket.json.send(msg);
};
var disconnectTimeout; var disconnectTimeout;
socket.once('connect', function () { socket.once('connect', function () {
@ -228,7 +235,7 @@ function handshake()
} }
pad.collabClient.setChannelState("CONNECTED"); pad.collabClient.setChannelState("CONNECTED");
sendClientReady(true); pad.sendClientReady(true);
}); });
socket.on('disconnect', function (reason) { socket.on('disconnect', function (reason) {
@ -246,7 +253,6 @@ function handshake()
} }
}); });
var receivedClientVars = false;
var initalized = false; var initalized = false;
socket.on('message', function(obj) socket.on('message', function(obj)
@ -286,7 +292,7 @@ function handshake()
} }
//if we haven't recieved the clientVars yet, then this message should it be //if we haven't recieved the clientVars yet, then this message should it be
else if (!receivedClientVars) else if (!receivedClientVars && obj.type == "CLIENT_VARS")
{ {
//log the message //log the message
if (window.console) console.log(obj); if (window.console) console.log(obj);
@ -426,6 +432,30 @@ var pad = {
{ {
return pad.myUserInfo.name; return pad.myUserInfo.name;
}, },
sendClientReady: function(isReconnect, messageType)
{
messageType = typeof messageType !== 'undefined' ? messageType : 'CLIENT_READY';
sendClientReady(isReconnect, messageType);
},
switchToPad: function(padId)
{
var options = document.location.href.split('?')[1];
var newHref = "/p/" + padId;
if (options != null)
newHref = newHref + '?' + options;
if(window.history && window.history.pushState)
{
$('#chattext p').remove(); //clear the chat messages
window.history.pushState("", "", newHref);
receivedClientVars = false;
sendClientReady(false, 'SWITCH_TO_PAD');
}
else // fallback
{
window.location.href = newHref;
}
},
sendClientMessage: function(msg) sendClientMessage: function(msg)
{ {
pad.collabClient.sendClientMessage(msg); pad.collabClient.sendClientMessage(msg);