【发布时间】:2017-04-29 20:40:45
【问题描述】:
我尝试使用 Node.js 和 socket.io 通过 web 构建一个 bash 终端,我需要知道命令是否完成。像“cd someDirectory”这样的命令不会发出输出,我不知道它是否完成。如何知道命令是否完成?如果可以为每个命令设置超时? 我有以下代码:
var shell = spawn('/bin/bash');
var stdin = shell.stdin;
shell.on('exit', function() {
socket.disconnect();
});
shell.on('close',function(){
console.log('Close');
});
shell['stdout'].setEncoding('ascii');
shell['stdout'].on('data', function(data) {
socket.emit('stdout', data);
});
shell['stderr'].setEncoding('ascii');
shell['stderr'].on('data', function(data) {
socket.emit('stderr', data);
});
socket.on('stdin', function(command) {
stdin.write(command+"\n") || socket.emit('disable');
});
stdin.on('drain', function() {
socket.emit('enable');
});
stdin.on('error', function(exception) {
socket.emit('error', String(exception));
});
提前致谢。
【问题讨论】: