Minimum Websocket Nodejs Tail Example

不羁岁月 提交于 2020-07-16 16:12:39

问题


I'm trying to create a stream of data to the browser using websocket. The data is the output of a log file. (tail -f filename) Using node js, I've manage to log into stdout, but I haven't been able to create the server and create the client (js/html) code to create a websocket and receive all the output of this child process. Can anyone help me?

NODE.JS SERVER OUTPUTTING TAIL TO STDOUT (as seen in 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);
});

My goal is to have the simplest stream possible. Any other simple solution is well received for this. Thanks.


回答1:


This simple?

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);

Connect to the server and you'll get your tail. You'll have to watch for timeouts on the client side, depending on your browser, if the tail goes idle.

If you want to access this data from javascript within the browser, consider using socket.io as this will use the best method the browser has available to access the stream (websocket, long poll, flash etc.). If you need a client javascript example, I can post that too.




回答2:


This seems like an old question & very possibly the problem is already solved, but in case it isn't here is a gist https://gist.github.com/867575 .

it uses socket.io and instead of spawning "tail -f" processes(which takes more memory), fs.watchFile is used.




回答3:


Here's a simple sample I mostly grabbed from this gist

First, switch into an empty directory

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

Then, install what's needed

npm install socket.io               

Run it like this

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

After it's running, open a browser at

http://localhost:8000

Here are the files you need:

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>


来源:https://stackoverflow.com/questions/3499152/minimum-websocket-nodejs-tail-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!