Browse files

Merge branch 'collab'

Conflicts:
	chat.js
	chat_client.html
  • Loading branch information...
2 parents 109a3b5 + 93ad550 commit a053df62433b07bc84859a2cbe7ffccc95aa66e4 @pksunkara committed Sep 18, 2011
Showing with 44 additions and 0 deletions.
  1. +31 −0 editor.js
  2. +13 −0 public/javascripts/collab.js
View
31 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);
+ });
+};
View
13 public/javascripts/collab.js
@@ -20,6 +20,19 @@ sendChat = function() {
e.value = '';
}
+key_handler = function (key) {
+ console.log('{'+cpos+','+ver+','+substr+','+e+'}');
+ socket.emit('text edit', {cursor:cpos, version:ver, substring:substr, edit:e});
+});
+
+socket.on('text edit', function (data){
+ console.log('text-edit: '+data);
+ var text = acee.getValue();
+ text = text.substr(0,data.cpos-1) + data.edit + text.substr(data.cpos -1 + data.edit.length);
+ ver += 1;
+ acee.setValue(text);
+});
+
socket.on('nickname?', function(data){
var nickname = prompt('Your nickname?');
socket.emit('nickname', nickname);

0 comments on commit a053df6

Please sign in to comment.