0

好的,我的 node.js 脚本中有一个使用事件流模块的函数。

我可以使用下面的简单脚本检查行数据,但我也希望能够使用每一行。我想要的例子

line(0)  --- first line of stdout 
line(1)  --- second line of stdout
ect, ect

我目前正在使用的脚本

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');


        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');


        }

    })
);   
}

然后我就这样称呼它

pinstripe();

这像预期的那样工作,但我如何制作这个功能,所以我可以像这样使用它..

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(line(0));   //i want to refrence
        child.stdin.write(line(1));   //each line like so 

        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(line(2));   //and here


        }

    })
);   
}

显然这不起作用,但我怎样才能使它起作用

4

1 回答 1

0

尝试这个:

function pinstripe() {
  var index = 0,
      lines = [];

  es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
      console.log(line);
      if (index <= 2) {
        lines[index++] = line;
      }
      if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(lines[0]);
        child.stdin.write(lines[1]);
        // set index = 0 to start over
      }
      if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(lines[2]);
        // set index = 0 to start over
      }
    })
  );
}
于 2014-04-03T21:00:28.030 回答