简体   繁体   English

Node.js端口同时侦听和从stdin读取

[英]Node.js port listening and reading from stdin at the same time

I have a socket server in Node.js and I'd like to be able to read from stdin at the same time the server is listening. 我在Node.js中有一个套接字服务器,我希望能够在服务器侦听的同时从stdin读取。 It works only partially. 它仅部分起作用。 I'm using this code: 我正在使用此代码:

process.stdin.on('data', function(chunk) {
    for(var i = 0; i < streams.length; i++) {
        // code that sends the data of stdin to all clients
    }
});

// ...

// (Listening code which responds to messages from clients)

When I don't type anything, the server responds to messages of the clients, but when I start typing something, it isn't until I press Enter that it continues with this task. 当我不输入任何内容时,服务器会响应客户端的消息,但是当我开始输入内容时,直到按Enter才继续执行此任务。 In the time between starting to type something and pressing Enter, the listening code seems to be suspended. 从开始键入内容到按Enter的这段时间内,侦听代码似乎已暂停。

How can I make the server still respond to clients while I'm typing in stdin? 在输入stdin时,如何使服务器仍对客户端响应?

I just wrote a quick test and had no problems processing input from stdin and http server requests at the same time, so you'll need to provide detailed example code before I can help you. 我只是编写了一个快速测试,没有问题,无法同时处理来自stdin和http服务器请求的输入,因此您需要提供详细的示例代码,然后才能为您提供帮助。 Here's the test code which runs under node 0.4.7: 这是在节点0.4.7下运行的测试代码:

var util=require('util'),
    http=require('http'),
    stdin=process.stdin;

// handle input from stdin
stdin.resume(); // see http://nodejs.org/docs/v0.4.7/api/process.html#process.stdin
stdin.on('data',function(chunk){ // called on each line of input
  var line=chunk.toString().replace(/\n/,'\\n');
  console.log('stdin:received line:'+line);
}).on('end',function(){ // called when stdin closes (via ^D)
  console.log('stdin:closed');
});

// handle http requests
http.createServer(function(req,res){
  console.log('server:received request');
  res.writeHead(200,{'Content-Type':'text/plain'});
  res.end('success\n');
  console.log('server:sent result');
}).listen(20101);

// send send http requests
var millis=500; // every half second
setInterval(function(){
  console.log('client:sending request');
  var client=http.get({host:'localhost',port:20101,path:'/'},function(res){
    var content='';
    console.log('client:received result - status('+res.statusCode+')');
    res.on('data',function(chunk){
      var str=chunk.toString().replace(/\n/,'\\n');
      console.log('client:received chunk:'+str);
      content+=str;
    });
    res.on('end',function(){
      console.log('client:received result:'+content);
      content='';
    });
  });
},millis);

Are you running your script as a background process using & ? 您是否正在使用&将脚本作为后台进程运行? Otherwise the console is the controlling terminal of the process, and is probably sending a SIGSTOP message when you're typing to avoid race conditions or something. 否则,控制台是该过程的控制终端,在键入时可能会发送SIGSTOP消息,以避免出现竞争情况或其他情况。

Try running the process as node myprocess.js & 尝试将流程作为node myprocess.js &运行node myprocess.js &

If it still happens, try nohup node myprocess.js & . 如果仍然发生,请尝试nohup node myprocess.js &

http://en.wikipedia.org/wiki/Signal_(computing) http://en.wikipedia.org/wiki/Signal_(computing)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM