为什么Node.js等待从child_process.spawn发布STDOUT流数据?

时间:2016-10-26 02:12:10

标签: node.js electron spawn

我刚刚开始考虑在Electron中构建应用,所以如果我以一种不合适的方式使用这个问题中的单词构造,我会提前道歉。

在学习的过程中,我只是尝试将从过去创建的IRC机器人收集的所有STDOUT数据发送到应用程序中的div元素。

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Bot World!</title>
  </head>
  <body>
    <h1>Bot World!</h1>
    <button type="button" id="bot_button">Run Bot</button>
    <p id="output"></p>
  </body>

<script>
  document.getElementById('bot_button').addEventListener("click", function (e) {

    var spawn = require('child_process').spawn;
    var child = spawn('ruby' , ['/home/syncthetic/Projects/Box/bots/hackthissite']);

    child.stdout.on('data', function(data) {
      console.log('stdout: ' + String(data));
      document.getElementById('bot_button').innerHTML += String(data) + '</b>';
    });

    child.stderr.on('data', function(data) {
      console.log('stdout: ' + String(data));
      document.getElementById('bot_button').innerHTML += String(data) + '</b>';
    });

    child.on('exit', function(code) {
      console.log('stdout: ' + String(data));
      document.getElementById('bot_button').innerHTML += String(data) + '</b>';
    });

 })
 </script>
 </html>

main.js

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL(`file://${__dirname}/index.html`)
  mainWindow.webContents.openDevTools()

  mainWindow.on('closed', function () {
    mainWindow = null
  })

}


app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

package.json只是调用main.js文件

IRC Bot的来源:https://github.com/syncthetic/Box/

似乎无论我连接到哪个网络,Electron应用程序都会等待特定的字节数,然后发布到console.log并附加到output {{1} }。

我读到Node的div命令会导致它将数据读入exec,而buffer则允许您实时访问该数据。

当程序本身将数据推送到spawn时,如何让它实时发布到应用程序?

为了记录,我也尝试使用STDOUT,但它没能像我预期的那样按照我的意愿行事。

1 个答案:

答案 0 :(得分:1)

我设法找到了自己答案的解决方案!

显然class RatingComponent { ngOnInit() { myRating = Array(numberOfStarsFromSomewhere).fill('whatever'); } } <i *ngFor="let rating of myRating" class="fa fa-star"></i> Ruby,毫无疑问,默认情况下,其他应用程序/语言会将数据发送到缓冲区中的Python

专门为解决正在运行的STDOUT脚本的问题,我必须使用以下行启用Ruby

STDOUT syncing