Electron.js - 将子进程的生成标准输出数据实时发送到渲染器



我有一个简单的节点子进程,它调用一个脚本,该脚本需要时间来输出一些信息(有点像ping的工作方式(。

let command = spawn(
execPath,
[...args],
{ cwd: null, detached: false }
);

然后我为stdout做一个标准的console.log:

command.stdout.on("data", (stdout) => {
console.log("Realtime Output: ", stdout.toString());
});

问题是,我想把这个实时输出发送回渲染器进程,并在前端显示它。我尝试在command.stdout.on()中添加一个ipcRenderer.send(),但不起作用,前端在console.log中显示undefined

有什么办法做到这一点吗?

您必须通过webContents 从mainWindow发送stdout

mainWindow.webContents.send('output', stdout.toString())

最新更新