简体   繁体   English

SOCKET.IO - console.log 正在打印所需的输出而不是 io.emit

[英]SOCKET.IO - console.log is printing desired output but not io.emit

I'm using socket.io to stream output from command line to html webpage.我正在使用 socket.io 将输出从命令行流式传输到 html 网页。

SERVER:服务器:

  var app = require('express')();
    var http = require('http').createServer(app);
    var io = require('socket.io')(http);
    const { spawn } = require("child_process");
    const ls = spawn("mycommand", ['-l 500'], {shell: true});
    
    app.get('/', (req, res) => {
      res.sendFile(__dirname + '/index.html');
    });
    
    
    
    io.on('connection', (socket) => {
      socket.on('chat message', (msg) => {
        ls.stdout.on("data", data => {
        io.emit(`${data}`, msg);
    });
      });
    });
    
    
    http.listen(3000, () => {
      console.log('listening on *:3000');
    });

CLIENT:客户:

<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>



<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
  $(function () {
    var socket = io();
    $('form').submit(function(e){
      e.preventDefault(); // prevents page reloading
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
    socket.on('chat message', function(msg){
      $('#messages').append($('<li>').text(msg));
    });
  });
</script>


  </body>
</html>

I'm not getting output on my HTML page when using io.emit() in my SERVER code but if I instead use console.log() desired output is printed on my command console but not on HTML.在我的 SERVER 代码中使用io.emit()时,我的 HTML 页面上没有输出,但是如果我改为使用console.log() ,则所需的输出会打印在我的命令控制台上,而不是打印在 HTML 上。 Where is the bug that is not allowing me to display output on my HTML?不允许我在 HTML 上显示输出的错误在哪里?

My objective is for server to stream output to the client from command line when user submits message to the server.我的目标是当用户向服务器提交消息时,服务器将输出从命令行流式传输到客户端。

Based on your comments, you can start listening to the stream on behalf of one particular client when it receives a chat message like this:根据您的评论,您可以在收到如下聊天消息时代表某个特定客户开始收听流:

io.on('connection', (socket) => {
  socket.on('chat message', (msg) => {
    function listener(data) {
        // send data from ls back to cilent
        socket.emit('lsdata', data);
    }
    ls.stdout.on('data', listener);
  });
  // when socket disconnects, remove the listener
  socket.on('disconnect', () => {
    ls.stdout.off('data', listener);
  });
});

Then, the client code needs a listener for the lsdata event:然后,客户端代码需要一个lsdata事件的监听器:

socket.on('lsdata', function(msg){
  $('#messages').append($('<li>').text(msg));
});

Note, there are still lots of your requirements that are unclear here because this starts listening on any chat message and will create duplicate listeners if it receives multiple chat messages from the same client (which does not seem right - but it's not clear how you want that to work).请注意,这里仍然有很多您的要求不清楚,因为这会开始侦听任何聊天消息,并且如果它从同一客户端收到多条聊天消息,则会创建重复的侦听器(这似乎不正确 - 但不清楚您想要什么工作)。 It seems more like the client should send a 'startListening' message and then maybe also have a 'stopListening' message it could send if it wanted to unplug from the data stream.看起来更像是客户端应该发送一个'startListening'消息,然后如果它想从数据流中拔出,它可能还会发送一个'stopListening'消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM