New at stackoverflow and new with Node.
I have one simple socket server and one web server.
I want the web server to send a message to the socket server if someone connected the web server.
Browser <=> Web Server/Socket Client <=> Socket Server
I created the server like this :
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/');
This code works but the web server sending the message to the socket server twice. Did I do something wrong maybe with the callback or something? Any help would be appreciated.