簡體   English   中英

在使用Node-Webkit執行程序時將stdout寫入div?

[英]Write stdout to a div as program executes with Node-Webkit?

在我的Node-Webkit應用程序中,我使用exec()執行子應用程序。 該應用程序將一些調試信息輸出到外殼。

問題是stdout僅在子應用程序退出后才發送文本。

當子應用程序正在運行時,是否可以使用stdout信息更新div元素?

var exec = require('child_process').exec;

exec(executableFile,  function (error, stdout, stderr) {

    if (stdout) {
        // this only updates after the app exits
        console.log('stdout: ' + stdout);
        $('#console-output').append('stdout: ' + stdout +'<br/>');
    }

    if (error !== null) {
      console.log('exec error: ' + error);
    }

});

您需要使用spawn而不是exec

Spawn將數據作為流發送回去,因此您可以使用stdout.on(fn)對其進行stdout.on(fn)

var spawn = require('child_process').spawn;

var proc = spawn(executableFile);

proc.stderr.on('data', function(stderr) {
    console.log('exec error: ' + stderr); 
});

proc.stdout.on('data', function(stdout) {
    $('#console-output').append('stdout: ' + stdout.toString() + '<br/>');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM