Created
March 14, 2010 21:49
-
-
Save jgoulah/332259 to your computer and use it in GitHub Desktop.
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
| // require the node.js system and filesystem modules | |
| var sys = require('sys'), | |
| fs = require('fs'); | |
| var Module = this.Module = function(data, connection){ | |
| // connection is the instance of websocket::Connection | |
| }; | |
| Module.prototype.onData = function(data, connection) { | |
| // client sends 'start' when the socket first connects | |
| if (data == 'start'){ | |
| var monitor_file = function(filename) { | |
| var cmd = process.createChildProcess("tail", ["-f", filename]); | |
| cmd.addListener("output", function(data) { | |
| // break up JSON objects and iterate over them | |
| var lines = data.split("\r"); | |
| for (var entry in lines) { | |
| try { | |
| // turn the json into an object | |
| var pdata = eval('(' + lines[entry] + ')'); | |
| // output to terminal | |
| sys.puts(pdata['text']); | |
| // send it over the socket | |
| connection.send(pdata['user']['screen_name'] +': ' + pdata['text']); | |
| sys.puts(''); | |
| } catch (err) {} | |
| } | |
| }); | |
| }; | |
| monitor_file("/home/jgoulah/sample.json"); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment