Skip to content
Browse files

collab server

  • Loading branch information...
1 parent 18589fe commit 93ad5508d6044c35e496df42528114ee182f7d69 @nihar19 nihar19 committed Sep 18, 2011
Showing with 51 additions and 3 deletions.
  1. +2 −2 chat.js
  2. +18 −1 chat_client.html
  3. +31 −0 editor.js
View
4 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'});
});
-}
+};
View
19 chat_client.html
@@ -1,6 +1,6 @@
<script src="/socket.io/socket.io.js"></script>
<script>
-
+var ver = 0;
var socket = io.connect("http://localhost");
socket.on('asking name', function(data){
console.log(data);
@@ -13,4 +13,21 @@
console.log(data.name + ' : ' + data.msg);
});
+function key_handler(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);
+});
+
</script>
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);
+ });
+};

0 comments on commit 93ad550

Please sign in to comment.
Something went wrong with that request. Please try again.