如何从导出循环中运行nodejs spawn

时间:2017-05-27 10:12:12

标签: node.js spawn

我正在尝试从相机模块连续流式传输相机照片。你知道如果在前一个spawn完成后启动另一个spawn吗? 目前我收到一个错误,即“退出”事件由子进程执行时未找到“startCamera”函数。

var spawn = require('child_process').spawn;
module.exports = {
cameraProcess: null,
startCamera: function () {
    var isWin = /^win/.test(process.platform);
    var path = require('path');
    if (isWin) {
        this.cameraProcess = spawn(path.join(__dirname, 'vfwgrab', 'VFWGrab.exe'));
    } else {
        var args = ["-w", "640", "-h", "480", "-o", "./public/stream/stream.jpg", "-t", "999999999", "-tl", "2000"];
        this.cameraProcess = spawn('raspistill', args);
    }
    this.cameraProcess.on('exit', this.loopCamera);
},
loopCamera: function (code) {
    this.startCamera(); //start the next spawn once the previous finished
}
};

1 个答案:

答案 0 :(得分:0)

现在我通过将函数移出导出然后在导出中引用它来解决这个问题。如果这是推荐的方式,请告诉我。

var spawn = require('child_process').spawn;
var cameraProcess = null;
var startCamera = function () {
  var isWin = /^win/.test(process.platform);
  var path = require('path');
  if (isWin) {
    this.cameraProcess = spawn(path.join(__dirname, 'vfwgrab', 'VFWGrab.exe'));
  } else {
    var args = ["-w", "640", "-h", "480", "-o", "./public/stream/stream.jpg", "-t", "999999999", "-tl", "2000"];
    this.cameraProcess = spawn('raspistill', args);
  }
  this.cameraProcess.on('exit', loopCamera);
}
var loopCamera = function (code) {
  startCamera();
}
module.exports = {
  startCamera: startCamera,
};