如何发送控件C node.js和child_processes [英] How to send control C node.js and child_processes

查看:58
本文介绍了如何发送控件C node.js和child_processes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想发送给child_process,例如ping 8.8.8.8-t,即无限数量的ping。还有一些迭代,我想停止该命令并执行一个新的迭代,但是在这种情况下,我不想杀死一个子进程。

Hello I want to send to the child_process, for example, ping 8.8.8.8-t, that is, an infinite number of ping. And some of the iterations I want to stop this command and execute a new, but in this case I do not want to kill a child process.

示例:

var spawn = require('child_process').spawn('cmd'),
    iconv = require('iconv-lite');

spawn.stdout.on('data', function (data) {
    console.log('Stdout: ', iconv.decode(data, 'cp866'));
});

spawn.stderr.on('data', function (data) {
    console.log('Stderr: ', iconv.decode(data, 'cp866'));
});

spawn.stdin.write('ping 8.8.8.8 -t'+ '\r\n');

spawn.stdin.write(here control-c...); // WRONG

spawn.stdin.write('dir' + '\r\n');


推荐答案

我找到了您以前的问题。看起来您正在尝试从node.js内部创建/模拟终端。您可以使用 readline 从终端进行读写。

I found your previous question. Looks like you are trying to create/emulate a terminal from within node.js. You can use readline for reading and writing from a terminal.

要编写控制字符,您可以从其文档

To write control character, you can see the example from its docs :

  rl.write('Delete me!');
  // Simulate ctrl+u to delete the line written previously
  rl.write(null, {ctrl: true, name: 'u'});

要直接回答问题,要传递特殊字符,您需要传递其ASCII值。 Ctrl + C 变为ASCII字符0x03。值取自此处

To directly answer the question, to pass special characters you will need to pass their ASCII values. Ctrl + C becomes ASCII character 0x03. Value taken from here.

  spawn.stdin.write("\x03");

这篇关于如何发送控件C node.js和child_processes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆