2016-06-10 4 views
0

내가 TCP 소켓 서버와 통신 HTTP 서버와 같은 노드 JS 응용 프로그램을 만들고, 코드있어 HTTP 서버로 및 TCP 소켓 클라이언트로는 다음과 같이의 모양 :노드 JS 한 번에

var http = require('http'); 
var net = require('net'); 
var url = require('url') ; 
const PORT=8080; 
var client = new net.Socket(); 
    client.connect(9000,'xxx.xxx.xxx.xxx', function(){ 
    console.log('Connected'); 
}); 
function handleRequest(request, response){ 
    var qo = url.parse(request.url,true).query; 
     if(typeof qo.q=="undefined"){ 
      response.end("ERROR"); 
     }else{ 
      client.write(decodeURIComponent(qo.q)); 
      client.on('data',function(data){ 
      response.writeHead(200, {'Content-Type': 'text/html','Access-Control-Allow-Headers':'Content-Type,Access-Control-Allow-Headers,Access-Control-Allow-Origin,X-Powered-ByX-Powered-By','Access-Control-Allow-Origin': '*','X-Powered-By':'Poltak ganteng'}); 
      response.end(data); 
      }); 
     } 
} 

var server = http.createServer(handleRequest); 
server.listen(PORT, function(){ 
     console.log("Server listening on: http://localhost:%s", PORT); 
}); 

내가 해요 한 번에 여러 요청을 처리하기 위해 코드가 제대로 작동하지 않을까 걱정됩니다. 여러 요청을 처리하고 요청자에게 올바른 응답을 얻을 수있는 방법이 있습니까?

답변

0

당신은 소켓 연결에 req/res를 사용하고 싶습니다. 각각의 클라이언트에서 uid를 보내고 싶습니다. 다른 서비스는 req/res와 일대일 관계를 유지하기 위해 동일한 uid로 응답합니다. 보장 된 통일성을위한 다른 선택권이 없다.

0

노드 요청간에 공유해야하는 TCP 클라이언트 (또는 클라이언트)로 값 비싼 리소스가 있으므로 socket-pool이 도움이 될 수 있습니다.