Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Almost finished

  • Loading branch information...
commit e48650d041832cf8716d3e4b3f96d7d5247fc69a 1 parent ebc1cbc
@pksunkara authored
Showing with 42 additions and 105 deletions.
  1. +21 −7 app.js
  2. +0 −31 editor.js
  3. +19 −17 public/javascripts/collab.js
  4. +2 −50 views/code.jade
View
28 app.js
@@ -49,18 +49,24 @@ app.get('/join', function(req, res){
app.get('/code/:id', function(req, res){
res.render('code', {
- title: 'CodeCollab'
+ title: 'CodeCollab',
+ code: codeToCollab
});
});
+app.listen(3000);
+
// socket I/O functionality
-var sessions = [];
+var sessions = [],
+ history = [],
+ docVersion = 0;
io.sockets.on('connection', function(socket){
socket.emit('nickname?', {});
socket.on('nickname', function(data){
data = (data===null)? randomString() : data;
+ socket.emit('version', {version: docVersion, text: codeToCollab});
socket.emit('members', sessions);
io.sockets.emit('join', {name: data, msg: data+' has joined the session'});
socket.set('nickname', data);
@@ -77,11 +83,17 @@ io.sockets.on('connection', function(socket){
sessions.splice(sessions.indexOf(nickname), 1);
});
});
-})
-
-// Start listening to server
-
-app.listen(3000);
+ socket.on('edit', function(data){
+ tmp = {cursor: data.cursor, len: (data.edit.length - data.sbstr.length)}
+ for(i=docVersion-data.version-1; i>=0; i--) {
+ if(tmp.cursor >= history[i].cursor)
+ tmp.cursor += history[i].len;
+ }
+ data.cursor = tmp.cursor;
+ history.push(tmp);
+ io.sockets.emit('edit', data);
+ });
+});
// Some useful functions
@@ -92,3 +104,5 @@ function randomString() {
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
+
+var codeToCollab = "io.sockets.on('connection', function(socket){\n socket.emit('nickname?', {});\n socket.on('nickname', function(data){\n data = (data===null)? randomString() : data;\n socket.emit('members', sessions);\n io.sockets.emit('join', {name: data, msg: data+' has joined the session'});\n socket.set('nickname', data);\n sessions.push(data);\n });\n socket.on('chat', function(data){\n socket.get('nickname', function(err, nickname){\n io.sockets.emit('chat', nickname+': '+data);\n });\n });\n socket.on('disconnect', function(){\n socket.get('nickname', function(err, nickname){\n io.sockets.emit('quit', {name: nickname, msg: nickname+' has quit the session'});\n sessions.splice(sessions.indexOf(nickname), 1);\n });\n });\n socket.on('edit', function(data){\n tmp = {cursor: data.cursor, len: (data.edit.length - data.sbstr.length)}\n for(i=docVersion-data.version-1; i>=0; i--) {\n if(tmp.cursor >= history[i].cursor)\n tmp.cursor += history[i].len;\n }\n data.cursor = tmp.cursor;\n history.push(tmp);\n io.sockets.emit('edit', data);\n });\n});"
View
31 editor.js
@@ -1,31 +0,0 @@
-
-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
36 public/javascripts/collab.js
@@ -1,6 +1,10 @@
var socket = io.connect(window.location.origin);
var members = [];
+var docVersion = 0,
+ noNeed = [37, 38, 39, 40],
+ sel = '';
+
updateMembers = function() {
var e = $('.members')[0];
var r = members[0];
@@ -32,19 +36,27 @@ getCursor = function() {
return pos+column;
}
-key_handler = function (key) {
- console.log('{'+cpos+','+ver+','+substr+','+e+'}');
- socket.emit('text edit', {cursor:cpos, version:ver, substring:substr, edit:e});
+key_handler = function (event) {
+ var key = event.which || event.keyCode;
+ if(noNeed.indexOf(key)==-1 && !(event.altKey || event.ctrlKey)) {
+ var cpos = getCursor();
+ socket.emit('edit', {cursor: cpos, version: docVersion, sbstr: sel, edit: String.fromCharCode(key)});
+ return false;
+ }
}
-socket.on('text edit', function (data){
- console.log('text-edit: '+data);
+socket.on('edit', function (data){
var text = acee.getValue();
- text = text.substr(0,data.cpos-1) + data.edit + text.substr(data.cpos -1 + data.edit.length);
- ver += 1;
+ text = text.substr(0,data.cursor-1) + data.edit + text.substr(data.cursor-1+data.edit.length);
+ docVersion += 1;
acee.setValue(text);
});
+socket.on('version', function(data){
+ version = data.version;
+ acee.setValue(data.text);
+});
+
socket.on('nickname?', function(data){
var nickname = prompt('Your nickname?');
socket.emit('nickname', nickname);
@@ -66,13 +78,3 @@ socket.on('nickname?', function(data){
putChat(data);
});
});
-
-/*
-$("#editor").keypress(function() {
- alert("Handler for .keypress() called!");
-});
-*/
-function keyp(event) {
- alert("Key - "+event.keyCode+" called!");
- return true;
-}
View
52 views/code.jade
@@ -17,56 +17,8 @@ script(type='text/javascript')
small my web application server script
.row
.span11
- pre#editor(style='width:620px;height:500px', onkeypress="return keyp(event)")
- | var express = require('express'),
- |
- | var app = module.exports = express.createServer();
- |
- | // Configuration
- | app.configure(function(){
- | app.set('views', __dirname + '/views');
- | app.set('view engine', 'jade');
- | app.use(express.bodyParser());
- | app.use(express.methodOverride());
- | app.use(app.router);
- | app.use(express.static(__dirname + '/public'));
- | });
- |
- | app.configure('development', function(){
- | app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
- | });
- |
- | app.configure('production', function(){
- | app.use(express.errorHandler());
- | });
- |
- | // Routes
- | app.get('/', function(req, res){
- | res.render('index', {
- | title: 'CodeCollab'
- | });
- | });
- |
- | app.get('/new', function(req, res){
- | res.redirect('/code/' + uid.gen());
- | });
- |
- | app.get('/join', function(req, res){
- | var id = req.param('id', null);
- | console.log(id);
- | if(id==null)
- | res.redirect('/');
- | else
- | res.redirect('/code/' + id);
- | });
- |
- | app.get('/code/:id', function(req, res){
- | res.render('code', {
- | title: 'CodeCollab'
- | });
- | });
- |
- | app.listen(3000);
+ pre#editor(style='width:620px;height:500px', onkeypress="return key_handler(event)")
+ = code
.span3
pre.members
pre.chatbox
Please sign in to comment.
Something went wrong with that request. Please try again.