开发者

how a client will connect to node.js server based on a specified type

I have designed this server to broadcast to all clients when it has to and broadcast to specific clients when it has to. I started working on client code to connect to it based on the arguments set in there but i got a mixed up at the point when client A is supposed to connect when a message is meant for client A or client B needs to connect based on that argument. Below is the server code. A client pseudo code will be appreciated. thanks

//send messages to game clients and forward to bay 
var socket_cl = io.listen(server);     
    socket_cl.on('connection', function (client) {
        client.on('message', function (data) {
            broadcast(data); // broadcast to all clients
            // OR
            //search for kiosk / the right kiosk for machine which sent message
            //to be forwarded bayboard client
            //after this find the location of that kiosk and send
            JSON.parse(data);
            if (data.type == 'bb_message') {
                var kiosk = readerEngine.getKiosk(client.request.socket.remoteAddress);
                bayboard_location_map[kiosk.location_id].send(data.message);
            }
            socket.clients[id].send(data); //send message to bayclients
        });
        client.on('disconnect', function () {开发者_如何学Go
            console.log('Client Disconnected.');
        });


I think you mean you want to send data to a particular client? Your server code looks like its going to do a lookup based on an id that you pass from the client?

Such that:

// Client side
if ( we want to send to a particular client ) {
    socket.send({destination: desination_id, payload: payload});
} else {
    socket.send({destination: 'all', payload: payload});
}

// Server side
client.on('message', function (data) {
    if (data.destination == 'all') {
        socket.broadcast(data.payload);
    } else {
        var destination = lookupDestination(data.destination);
        destination.send(data.payload)
    }
});


I'm not sure it is as simple as that with TCP/IP sockets, as once a client socket is opened, in the server, it can be used to communicate only with the specific client.

What I would do in this scenario, is to loop on all open sockets (client connections) and send each, as an alternative to a broadcast.

With kind regards,

Oren

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜