Created
July 5, 2012 17:10
-
-
Save jankuca/3054961 to your computer and use it in GitHub Desktop.
OT in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var area = document.getElementById('area'); | |
var buffer = []; | |
var position = 0; | |
var last_id = 1; | |
var socket = io.connect('http://localhost:5000'); | |
socket.on('operation', function (operation) { | |
}); | |
area.onkeyup = function (e) { | |
setTimeout(function () { | |
last_id += 1; | |
var key = e.keyCode; | |
if (key >= 48 && key < 112 || key > 250) { | |
var str = String.fromCharCode(key); | |
var operation = { | |
'actions': [ | |
{ | |
'i': position, | |
'l': str.length, | |
'c': str, | |
'p': last_id | |
} | |
] | |
}; | |
socket.emit('operation', operation); | |
position += str.length; | |
area.value = ''; | |
} | |
}, 0); | |
}; | |
function transform(received, previous) { | |
received.actions.forEach(function (received_action) { | |
var pos = received_action.position; | |
previous.actions.forEach(function (previous_action) { | |
if (previous_action.position <= pos) { | |
pos += previous_action.length; | |
} | |
}); | |
received_action.position = pos; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea id="area"></textarea> | |
<div id="rendering"></div> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="/client.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var http = require('http'); | |
var server = http.createServer(function (req, res) { | |
req.url = (req.url === '/') ? 'index.html' : req.url.substr(1); | |
try { | |
var html = fs.readFileSync(req.url, 'utf8'); | |
res.writeHead(200, { 'content-type': 'text/html; charset=UTF-8' }); | |
res.write(html); | |
} catch (err) { | |
res.writeHead(404); | |
} | |
res.end(); | |
}); | |
var io = require('socket.io').listen(server); | |
var operations = []; | |
io.sockets.on('connection', function (socket) { | |
socket.emit('init', operations); | |
socket.on('operation', function (data) { | |
var operation = { | |
actions: data['actions'].map(function (action) { | |
return { | |
position: action['i'], | |
length: action['l'], | |
content: action['c'] | |
} | |
}) | |
}; | |
console.log('received operation:', operation); | |
var last_id = data['p']; | |
for (var i = operations.length - 1; i >= 0; --i) { | |
var change = operations[i]; | |
if (change.id !== last_id) { | |
transform(operation, change); | |
} else { | |
break; | |
} | |
} | |
operations.push(operation); | |
// broadcast the transformed operation | |
// this works as an server acknowledgment for the origin | |
socket.broadcast.emit('operation', operation); | |
}); | |
}); | |
server.listen(5000); | |
function transform(received, previous) { | |
received.actions.forEach(function (received_action) { | |
var pos = received_action.position; | |
previous.actions.forEach(function (previous_action) { | |
if (previous_action.position <= pos) { | |
pos += previous_action.length; | |
} | |
}); | |
received_action.position = pos; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment