Created
October 18, 2012 15:10
-
-
Save mhuneke/3912458 to your computer and use it in GitHub Desktop.
Live Reload client-server
This file contains hidden or 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
// <script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script> | |
var socket = io.connect(); | |
socket.on('server:reload', function(message) { | |
document.location.reload(); | |
}); |
This file contains hidden or 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 io = require('socket.io').listen(app); | |
var fs = require('fs'); | |
function watchDirRecursive(directory, extensions, fn) { | |
fs.readdir(directory, function(err, files) { | |
if (err) throw err; | |
for (var i = 0; i < files.length; i++) { | |
var file = directory + '/' + files[i]; | |
var stat = fs.statSync(file); | |
if (stat.isDirectory() && files[i][0] != '.') { | |
watchDirRecursive(file, extensions, fn); | |
} else if (stat.isFile()) { | |
var extIdx = file.lastIndexOf('.'); | |
if (extIdx > 0 && extensions.indexOf(file.substr(extIdx+1)) > -1) { | |
fs.watchFile(file, fn); | |
} | |
} | |
} | |
}); | |
} | |
watchDirRecursive('.', ['html', 'css', 'js'], function() { | |
io.sockets.emit('server:reload'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment