From 93ad5508d6044c35e496df42528114ee182f7d69 Mon Sep 17 00:00:00 2001 From: Nihar V Settipalli Date: Sun, 18 Sep 2011 06:58:23 +0530 Subject: [PATCH] collab server --- chat.js | 4 ++-- chat_client.html | 19 ++++++++++++++++++- editor.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 editor.js diff --git a/chat.js b/chat.js index fbc26a8..17bad43 100644 --- a/chat.js +++ b/chat.js @@ -4,7 +4,7 @@ var CHAT_PORT = 3000 var io = require('socket.io').listen(CHAT_PORT), -io.sockets.on('connection', socketHandler) +io.sockets.on('connection', socketHandler); function socketHandler(socket){ socket.emit('asking name', {msg:'What\'s your name?'}); @@ -22,5 +22,5 @@ function socketHandler(socket){ socket.on('disconnect', function() { io.sockets.emit('message', {name:'GOD', msg:socket.get('name')+' has left the editing session'}); }); -} +}; diff --git a/chat_client.html b/chat_client.html index bc7d9f2..62e526f 100644 --- a/chat_client.html +++ b/chat_client.html @@ -1,6 +1,6 @@ diff --git a/editor.js b/editor.js new file mode 100644 index 0000000..6bfe56e --- /dev/null +++ b/editor.js @@ -0,0 +1,31 @@ + +var EDITOR_PORT=3001 + +var version = 0, +io = require('socket.io').listen(EDITOR_PORT); + +class Data { + var cursor; + var edit; +}; + +var history = new Array(); +Data temp; + +io.sockets('connection', socketHandler); + +function socketHandler(socket) { + socket.on('text edit', function(data){ + temp.cursor = data.cursor; + temp.edit = len(data.edit)-len(data.substring); + for(int i=version-data.version-1; i>=0; i--) { + if(temp.cursor >= history[i].cursor){ + temp.cursor += history[i].edit; + } + } + + data.cursor = temp.cursor; + history.push(temp); + io.sockets.emit('text edit', data); + }); +};