노드에서 stackoverflow 및 new에서 새로 추가되었습니다.노드 JS 실행 된 함수 두 번
하나의 간단한 소켓 서버와 하나의 웹 서버가 있습니다.
누군가 웹 서버에 연결되어 있으면 웹 서버가 소켓 서버에 메시지를 보내길 원합니다.
브라우저 < => 웹 서버/소켓 클라이언트 < => 소켓 서버
나는이 같은 서버 생성: 소켓 서버로 메시지를 전송
var http = require('http');
var net = require('net');
var HOST = '192.168.1.254';
var PORT = 8888;
var client = new net.Socket();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n', function(){
client.connect(PORT, HOST, function() {
console.log('Connected To: ' + HOST + ':' + PORT);
client.write('0109001' + "\n");
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
console.log('The Data: ' + data);
client.destroy();
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
});
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
이 코드는 작동하지만 웹 서버를 두번. 콜백 또는 무언가에 문제가 있습니까? 도움이 될 것입니다.
한 번만 요청을 보내시겠습니까? 모든 브라우저가하는 favicon 요청을 잊지 마십시오. – pfried
['console.log (req.url);'] (http://nodejs.org/api/http.html#http_request_url)을 추가하십시오. 매번 동일한 URL을 출력합니까? –