模拟http keep-alive connected以后就没反应了
发布于 2 个月前 作者 yakczh 293 次浏览 来自 问答

server-alive.js

const  net= require('net');
var htmlpage = '<h1>ok</h1>\r\n';
var first =true;
 
    var server = net.createServer( (client) => {     //'connection' listener
        client.setKeepAlive(true,9000);
        client.setTimeout(1000);
        client.setNoDelay(true);
        console.log('connection  connected');
        client.on('end', (err)=> {
        	console.log(err);
                  console.log('\t\t\ttime out end it ');
        });
        client.on('data', (data) =>{
        	console.log(data.toString());
  
        	 var state=(first)?'Keep-Alive':'Close';
        	 first=false;
                //  4495
            	  client.write(new Buffer("HTTP/1.1 200 OK\r\nConnection: "+state+"\r\nSet-Cookie: PHPSESSID=qn28r1v7n6l8h1odvvcdeobfp1; path=/; HttpOnly\r\nConent-Length: 13\r\n\r\n"+htmlpage));
              //   client.end();
        }).on('error',(err) =>{
               console.log(err);
        });
    });
    //开启端口的监听
    server.listen(80, function() { //''     listener
        console.log('server listening...');
    });

http-client.js

 const http = require('http');
var options={
  hostname: 'localhost',
  port: 80,
  path: '/',
  headers: {
      'Connection': 'Keep-Alive'
  }

}
const keepAliveAgent = new http.Agent({ keepAlive: true });
options.agent = keepAliveAgent;
http.request(options,  (resp) =>{
console.log(resp);
});

setTimeout(function(){
http.request(options,  (resp) =>{
console.log(resp);
});
	}, 8000);

客户端连接以后服务端只显示 connection connected 然后就没响应了 这是哪里的问题?

回到顶部