diff --git a/server.js b/server.js index 3b7b73d..33bbc90 100644 --- a/server.js +++ b/server.js @@ -96,11 +96,6 @@ app.get('/', isAuthenticated, function(req, res){ app.post('/', isAuthenticated, function(req, res){ winston.info('POST received'); var now = Date.now(); - // quick/dirty poc is to do a fileSystem.write to create a new file w/contents of req.body! - //fileSystem.writeFile(__dirname + '/note-data/test-' + now + '.txt', req.body, function(err){ - //if (err) { winston.error(err); } - //winston.info('new test file written'); - //}); // Overwrite allNotes.txt with new contents from client. fileSystem.readFile(notePath, 'utf-8', function(err, data){ if (err) { @@ -119,7 +114,6 @@ app.post('/', isAuthenticated, function(req, res){ }); } }); - // Stage and commit changes to allNotes.txt. res.status(204).send('POST received'); }); @@ -142,14 +136,45 @@ app.get('/logout', function(req, res){ var http = require('http').Server(app); var io = require('socket.io')(http); - io.on('connection', function(socket){ //winston.info('a user connected'); + fileSystem.readFile(notePath, {encoding: 'utf-8'}, function(err,data){ + if (!err){ + winston.info('successful file read'); + io.emit('sending allNotes',data); + } else { winston.error(err); } + }); + socket.on('sending allNotes', function(msg){ + winston.info('WS data received'); + var now = Date.now(); + // Overwrite allNotes.txt with new contents from client. + fileSystem.readFile(notePath, 'utf-8', function(err, data){ + if (err) { + winston.error(err); + } else { + fileSystem.writeFile(notePath, msg, 'utf-8', function(err) { + if (err) { winston.error(err); } + winston.info('new contents from client written to allNotes.txt'); + var exec = require('child_process').exec; + var cmd = 'cd note-data && git commit -am "Notes modified via websocket, ' + now + '"'; + winston.info(cmd); + exec(cmd, function(error, stdout, stderr) { + if (error) { winston.error(error); } + winston.info(stdout); + }); + }); + } + }); + }); socket.on('cm-save', function(msg){ winston.info(msg); }); }); +//setInterval(function() { + //io.emit('server event', Date.now()); +//},30000); + http.listen(3000, function() { winston.info("Started on PORT 3000"); }); diff --git a/views/index.hbs b/views/index.hbs index a5492f3..c672508 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -45,7 +45,8 @@
- + +
@@ -74,21 +75,35 @@ socket.emit('cm-save', 'codemirror save event'); } + var socket = io(); + //socket.on('server event', function(msg){ + //console.log(msg); + //}); + socket.on('sending allNotes', function(msg){ + //console.log(msg); + editor.getDoc().setValue(msg); + }); + + var userKeypress = false; + document.onkeydown = function(){ userKeypress = true; }; var typingTimer; editor.on("changes", function() { + if (!userKeypress) { return; }; // Do nothing if no keys hit yet, avoids infinite loop since 'changes' event fires after initial websocket content loading. clearTimeout(typingTimer); typingTimer = setTimeout( function() { //console.log(editor.getValue()); - var request = new XMLHttpRequest(); - request.onreadystatechange = function () { - var DONE = this.DONE || 4; - if (this.readyState === DONE){ - console.log('ajax is done.'); - } - }; - request.open('POST', '/', true); - request.send(editor.getValue()); + //var request = new XMLHttpRequest(); + //request.onreadystatechange = function () { + //var DONE = this.DONE || 4; + //if (this.readyState === DONE){ + //console.log('ajax is done.'); + //} + //}; + //request.open('POST', '/', true); + //request.send(editor.getValue()); + var socket = io(); + socket.emit('sending allNotes', editor.getValue()); }, 2000 );