I am writing an application on node.js, which is both an http and tcp server. tcl server works with certain clients - sends them commands and gets a response.
Necessary: When you receive a request via http - send a command to a specific client, receive a response from it and give this response in the request via http. All this needs to be done without blocking, i.e. to support the work of several clients via http and using several tcp clients.
//http server http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); //tcp server var clients = []; net.createServer(function (socket) { socket.name = socket.remoteAddress + ":" + socket.remotePort clients.push(socket); socket.on('data', function (data) {}); socket.on('error',function(error) { socket.end(); console.log(error); }); socket.on('close',function(had_error) {}); socket.on('end', function () {console.log('onend');}); }).listen(5000); Those. you need to somehow send the response object http to socket.on ('data', function (data) {}); where reading from the client's tcp socket occurs.
Any ideas on how to do this or some kind of architectural solution?
Thanks in advance for your reply.
Addition:
I have to additionally read the information from tcp clients and they are authorized on the server. And only when I send them a command then I wait for an answer right away
socket.on('data', function (data) { var imei = data.toString("binary"); //авторизация if (imei == 'xxxxxxxx') { var raw = '01'; this.imei = imei; var now = new Date(); var buff = new Buffer(raw, 'hex'); socket.write(buff); } //клиент уже прошело авторизацию. шлет нам какие-то данные } else if(this.imei && (data.length == 3 || data.length == 6)) { } else if (data[0] == 4 && data[2] > 0) { } else if (this.imei && data.length > 3) { //и где-то здесь нужно нам, наверное, поймать ответ от клиента после посылки ему команды });