Created
December 19, 2009 17:30
-
-
Save jpignata/260161 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
var tcp = require("tcp"); | |
var sys = require("sys"); | |
var posix = require("posix"); | |
var file = process.ARGV[2].toString(); | |
Array.prototype.remove = function(e) { | |
for(var i = 0; i < this.length; i++) | |
if(e == this[i]) this.splice(i, 1); | |
} | |
Array.prototype.each = function(fn) { | |
for(var i = 0; i < this.length; i++) fn(this[i]); | |
} | |
function Client(connection) { | |
this.name = null; | |
this.connection = connection; | |
} | |
var clients = []; | |
var server = tcp.createServer(function (socket) { | |
var client = new Client(socket); | |
clients.push(client); | |
socket.setTimeout(0); | |
socket.setEncoding("utf8"); | |
socket.addListener("close", function() { | |
clients.remove(client); | |
sys.puts("Closed connection from " + client.connection.remoteAddress); | |
}); | |
socket.addListener("connect", function() { | |
sys.puts("Received connection from " + client.connection.remoteAddress); | |
}) | |
process.watchFile(file, function(current, previous) { | |
posix.open(file, process.O_RDONLY, 438).addCallback(function(fd) { | |
if (fd) { | |
var size = current.size - previous.size; | |
posix.read(fd, size, previous.size, "utf8").addCallback(function(data, bytes_read) { | |
if (bytes_read > 0) { | |
clients.each(function(client) { | |
client.connection.send(data); | |
}); | |
} | |
}); | |
posix.close(fd); | |
} | |
}); | |
}); | |
}); | |
server.listen(7000); | |
sys.puts("tail -f'ing " + file + " on port 7000") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment