最小Websocket Nodejs尾示例

我正在尝试使用websocket为浏览器创build一个数据stream。 数据是日志文件的输出。 (尾-f文件名)使用节点JS,我已经设法login到标准输出,但我还没有能够创build服务器,并创build客户端(JS / HTML)的代码来创build一个WebSocket并接收所有的输出这个孩子的过程。 谁能帮我?

NODE.JS服务器输出尾部到标准输出(如http://snippets.dzone.com/posts/show/12067所示 )

var sys = require('sys') var spawn = require('child_process').spawn; var filename = process.ARGV[2]; if (!filename) return sys.puts("Usage: node <server.js> <filename>"); var tail = spawn("tail", ["-f", filename]); sys.puts("start tailing"); tail.stdout.on("data", function (data) { sys.puts(data); }); 

我的目标是有最简单的可能。 任何其他简单的解决scheme都很受欢迎。 谢谢。

这个简单?

 var sys = require('sys') var spawn = require('child_process').spawn; var filename = process.ARGV[2]; if (!filename) return sys.puts("Usage: node <server.js> <filename>"); var tail = spawn("tail", ["-f", filename]); http = require('http'); http.createServer(function (req, res) { sys.puts("new connection.."); res.writeHead(200, {'Content-Type': "text/plain;charset=UTF-8"}); tail.stdout.on("data", function (data) { res.write(data); }); }).listen(3000); 

连接到服务器,你会得到你的尾巴。 如果尾巴闲置,您将不得不在客户端观看超时,具体取决于您的浏览器。

如果你想从浏览器中的JavaScript访问这个数据,考虑使用socket.io,因为这将使用浏览器可用的最佳方法访问stream(websocket,长轮询,闪存等)。 如果你需要一个客户端的JavaScript例子,我也可以发布。

这似乎是一个古老的问题,很可能问题已经解决,但万一它不在这里是一个要点https://gist.github.com/867575

它使用socket.io而不是产生“tail -f”进程(需要更多的内存),使用fs.watchFile。

这里有一个简单的例子,我主要从这个要点中抓住

首先,切换到一个空目录

 mkdir socket-tail-app; cd socket-tail-app; 

然后,安装需要的东西

 npm install socket.io 

像这样运行

 node server.js /path/to/file/to/tail 

运行后,打开浏览器

 http://localhost:8000 

这里是你需要的文件:

server.js

 var http = require('http'), io = require('socket.io'), fs = require('fs'); var spawn = require('child_process').spawn; var filename = process.argv[2]; if (!filename) { console.log("Usage: node server.js filename_to_tail"); return; } // -- Node.js Server ---------------------------------------------------------- server = http.createServer(function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}) fs.readFile(__dirname + '/index.html', function(err, data){ res.write(data, 'utf8'); res.end(); }); }) server.listen(8000, '0.0.0.0'); // -- Setup Socket.IO --------------------------------------------------------- var io = io.listen(server); io.on('connection', function(client){ console.log('Client connected'); var tail = spawn("tail", ["-f", filename]); client.send( { filename : filename } ); tail.stdout.on("data", function (data) { console.log(data.toString('utf-8')) client.send( { tail : data.toString('utf-8') } ) }); }); console.log('Server running at http://0.0.0.0:8000/, connect with a browser to see tail output'); 

的index.html

 <!DOCTYPE html> <html> <head> <title>tail.js</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="//code.jquery.com/jquery-2.1.4.min.js"></script> <script src="//cdn.socket.io/socket.io-1.3.7.js"></script> <style> body { color: #1a2c37; font-family: 'Helvetica', sans-serif; font-size: 86%; padding: 2em; } #info { font-size: 120%; font-weight: bold; } #tail { border: 1px solid #ccc; height: 300px; padding: 0.5em; overflow: hidden; position: relative; overflow-y: scroll; } </style> </head> <body> <pre id="info"></pre> <pre id="tail"></pre> <script> var Application = function() { var socket = io.connect('http://127.0.0.1:8000/'); socket.on('connect', function() { console.log('Connected to:', socket.host); }); socket.on('message', function(message) { console.log('Received message:', message); if (message.filename) { $('#info').html( '$ tail -f ' + message.filename ); }; if (message.tail) { $('#tail').html( $('#tail').html() + message.tail ); bottom = $("#tail")[0].scrollHeight - $("#tail").height() $('#tail').scrollTop(bottom); } }); return { socket : socket }; }; $(function() { var app = Application(); }); </script> </body> </html>