admin管理员组文章数量:1336181
I want to send a hello message to a C++ based server that is listening over port "9090" once the message is sent, the server will do some job or task that will take some time, so i don't want to leave the app.get(); function block, how can i do that until getting a data from server then send back to the user as a response?
Basically what i want is to not reply the user, stay inside the loop while(close_connection_flag == false) until it set to true to leave the loop (until i get a tcp-reply data) then leave the loop and reply the user.
The code i have:
var net = require('net');
var tcp_client = new net.Socket();
var express = require('express');
var app = express();
var close_connection_flag = false;
var flag_raised_by_server = '';
app.get('/', function(req, res) {
tcp_client.on('connect', function(){
console.log('[+] Connected.');
tcp_client.write('HelloServerSideFrom:Client-Server');
});
while(close_connection_flag == false) {
tcp_client.on('data', function(data) {
switch(data){
case 'HelloClientServerFrom:Server':
close_connection_flag = true;
break;
case 'data-success':
close_connection_flag = true;
flag_raised_by_server = 'ds';
break;
case 'data-failure':
close_connection_flag = true;
flag_raised_by_server = 'df';
break
default:
// do nothing.
break;
}
});
}
res.end('flag raised by server is: ' + flag_raised_by_server);
tcp_client.destroy();
});
Note: I'm still newbie in Node.js
, so if you could please provide some ments or explanation of the example code if you provide a one, thanks.
I want to send a hello message to a C++ based server that is listening over port "9090" once the message is sent, the server will do some job or task that will take some time, so i don't want to leave the app.get(); function block, how can i do that until getting a data from server then send back to the user as a response?
Basically what i want is to not reply the user, stay inside the loop while(close_connection_flag == false) until it set to true to leave the loop (until i get a tcp-reply data) then leave the loop and reply the user.
The code i have:
var net = require('net');
var tcp_client = new net.Socket();
var express = require('express');
var app = express();
var close_connection_flag = false;
var flag_raised_by_server = '';
app.get('/', function(req, res) {
tcp_client.on('connect', function(){
console.log('[+] Connected.');
tcp_client.write('HelloServerSideFrom:Client-Server');
});
while(close_connection_flag == false) {
tcp_client.on('data', function(data) {
switch(data){
case 'HelloClientServerFrom:Server':
close_connection_flag = true;
break;
case 'data-success':
close_connection_flag = true;
flag_raised_by_server = 'ds';
break;
case 'data-failure':
close_connection_flag = true;
flag_raised_by_server = 'df';
break
default:
// do nothing.
break;
}
});
}
res.end('flag raised by server is: ' + flag_raised_by_server);
tcp_client.destroy();
});
Note: I'm still newbie in Node.js
, so if you could please provide some ments or explanation of the example code if you provide a one, thanks.
-
Shouldn't you have a
tcp_client.on('close')
event? – Louay Alakkad Commented Dec 20, 2015 at 17:08
2 Answers
Reset to default 2If I understood the question correctly, you want to send the HTTP response as soon as you've received a response from your downstream socket server.
First of all, the most important thing to consider: Socket connections -- like almost everything -- in Node.JS are asynchronous.
This has some very important consequences:
The call to
tcp_client.on('data', ...)
will not block your program execution until you've received some data. It will simply register a callback function that will (may) be called at some later point in time and then continue your program.This means that if you register a callback in your TCP client and then send the HTTP reponse, the HTTP response will be sent first, and your callback will be called at some later point in time.
There is no need to use a while loop for polling on your socket; simply use the
tcp_client.on('data', ...)
method once to register a callback function that is called as soon data is read from the socket.As
tcp_client.on(...)
is asynchronous, it will return immediately; if you do so in a loop, you'll basically spin endlessly and constantly register new event listeners. So, lose thewhile(close_connection_flag == false)
loop!If you want to wait with your HTTP response until you've received data on your TCP socket, simply put your
res.end(...)
call inside thetcp_client
callback.
All in all, I'd suggest something like this:
app.get('/', function(req, res) {
tcp_client.on('connect', function(){
console.log('[+] Connected.');
tcp_client.write('HelloServerSideFrom:Client-Server');
});
tcp_client.on('data', function(data) {
var flag_raised_by_server;
switch(data){
case 'HelloClientServerFrom:Server':
close_connection_flag = true;
break;
case 'data-success':
close_connection_flag = true;
flag_raised_by_server = 'ds';
break;
case 'data-failure':
close_connection_flag = true;
flag_raised_by_server = 'df';
break
default:
// do nothing.
break;
}
if (flag_raised_by_server) {
res.end('flag raised by server is: ' + flag_raised_by_server);
tcp_client.destroy();
}
});
});
Instead of setting close_connection_flag
to true, just close the connection right there and return to the user. What you're doing here is creating a memory leak with an infinite amount of listeners on the tcp_client
.
app.get('/', function(req, res) {
tcp_client.on('connect', function(){
console.log('[+] Connected.');
tcp_client.write('HelloServerSideFrom:Client-Server');
});
tcp_client.on('data', function(data) {
switch(data){
case 'HelloClientServerFrom:Server':
close_connection_flag = true;
break;
case 'data-success':
close_connection_flag = true;
flag_raised_by_server = 'ds';
break;
case 'data-failure':
close_connection_flag = true;
flag_raised_by_server = 'df';
break
default:
// do nothing.
break;
}
if (close_connection_flag) {
tcp_client.destroy();
res.end('flag raised by server is: ' + flag_raised_by_server);
}
});
});
本文标签:
版权声明:本文标题:javascript - How can i wait for data until it get received in Node.js from a TCP server while GET request is sent by the user? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742401709a2468013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论