The question is not actually that simple. I just try to ask simple question.
It was for node.js. The situation is when a user log in and go to a particular page.
He is automatically connected to a server through websocket setting.
There is a button on that page if the user want to get information design to serve for him. When he hit the button, his information is sent to a server.
At the server, node.js recorded connection. When it receive data it processes it by sending request to other server programs. When it get returned data, it sends that data back to the requester. Simple process.
However, there is problems. I don’t know how to send data back to that particular person. The code below just send data to everybody who is on the page:
var server = new WebSocketServer({port:8080}),
clients = ;
server.on('connection', function(ws) {
clients.push(ws);
ws.on('message', function(message, flags) {
// 1. server receive requested data
// 2. check database and user identity
// 3. if okay read file and return data accordingly
// 4. send data to the requester with a method below
for (var i = 0, j = clients.length; i < j; i += 1) {
clients[i].send(data);
}
});
});
So how to compare something like this:
if ( clients[i] === this.very.requester ) {
// send data to this person only
}
Thank you,
It is the person that made the request, to who the response it send back to. It works in the same way that a web page is sent only to the person who requests it.
Other than the data that’s sent back to the requester, is there some reason why you would want to keep an open connection back to the requester?
After helped people solved problem in another forum I realize that I could do it on a user machine. But it can be great if I can do it on a server as well.
Supposed it was a poker game, financial trading data, the user might want to always stay connected. Cut off connection is not a good idea. I notice that Chrome Beta for Android still received data in a sleep mode.
It seems then that you may be after a keep alive agent, that can help to keep the connection open for you.
Be careful of such things though. Connections are something that developers need to be frugal with, lest you lose the ability for others to connect to your server.