Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets Node.js端口同时侦听和读取stdin_Sockets_Node.js_Stdin - Fatal编程技术网

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

Sockets Node.js端口同时侦听和读取stdin,sockets,node.js,stdin,Sockets,Node.js,Stdin,我在Node.js中有一个socket服务器,我希望能够在服务器侦听的同时读取stdin。它只能部分起作用。我正在使用以下代码: 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

我在Node.js中有一个socket服务器,我希望能够在服务器侦听的同时读取stdin。它只能部分起作用。我正在使用以下代码:

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)
process.stdin.on('data',函数(块){
对于(变量i=0;i
当我不键入任何内容时,服务器会响应客户端的消息,但当我开始键入内容时,直到我按Enter键,服务器才会继续执行此任务。在开始键入内容和按Enter键之间的时间内,监听代码似乎被挂起


在输入stdin时,如何使服务器仍然响应客户端?

我刚刚编写了一个快速测试,在同时处理来自stdin和http服务器请求的输入时没有问题,因此在我帮助您之前,您需要提供详细的示例代码。以下是在节点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);

您是否使用
作为后台进程运行脚本?否则,控制台是进程的控制终端,当您键入以避免竞争条件或其他情况时,可能会发送一条
SIGSTOP
消息

尝试以
节点myprocess.js&

如果仍然发生,请尝试
nohup node myprocess.js&


+1,用于尝试复制,但未发现任何问题!我觉得代码看起来不错。非常感谢,但我必须说,我也可以用这个重现我的问题。它每0.5秒发送一次,但我一键入,它就被挂起。当我按Enter键时,消息将再次发送。提到我正在使用Windows可能会有所帮助?我记录了我在您的示例中看到的内容,您看到的是相同的吗?在Linux下运行时,我看不到同样的情况,因此Windows控制台很可能会阻塞线程。有关在Windows下运行节点的相关问题的解释,请参阅。@Rob Raisch:谢谢,这确实可能是问题所在。据我所知,没有解决办法。对吗?