Question

I'm new to nodejs so please forgive me if this question is stupid. When I write udp socket with Python, I could do:

socket.recvfrom(len)  # socket is udp
do_something_else
socket.recvfrom(len)

and receives a certain length of data one at a time. I've read node's doc about UDP, it seems this is the only way for a UDP socket to receive data.

Event: ‘message’
• msg Buffer object. The message
• rinfo Object. Remote address information
Emitted when a new datagram is available on a socket. msg is a Buffer and rinfo is an object with the
sender’s address information:

socket.on(’message’, function(msg, rinfo) {
    console.log(’Received %d bytes from %s:%d\n’,
    msg.length, rinfo.address, rinfo.port);
});

The difference is that if I use message event I don't know what this msg is. I'm not sure you understand, so for example, I'm communicating with a server, the first time server send me "enter your name" and I send my name back to it in do_something_else, then receive an id. By recvfrom I could control the process cause I know what will happen next but it's not obvious to me how to do this with message event.

Thank you.

Was it helpful?

Solution

I really don't understand your problem, can't you do something like this:

var states = {
        invalid_data_received: -1,
        nothing_received: 0,
        received_name: 1,
        received_some_data: 2
    },
    state = states.nothing_received;

socket.on(’message’, function(msg, rinfo) {
    msg = msg.toString();
    if (state === states.nothing_received) {
        do_something(msg);
    } else if (state === states.received_name) {
        do_something_else(msg);
    } else if (state === states.received_some_data) {
        do_even_more(msg);
    } else if (state === states.invalid_data_received) {
        // incorrect message received
    }
});

OTHER TIPS

check this chat demo. It uses hash to store users. It demonstrates the solution to your question.

var net = require("net");
var count = 0, users = {};
var server = net.createServer(function (conn){
var nickname;
conn.setEncoding('utf8');
conn.write(' > welcome to \033[92mnode-chat\033[39m!'
    + '\n > ' + count + ' other people are connected at this time.'
    + '\n > please write your name and press enter: ');
count++;
conn.on('data', function (data){
    data = data.replace('\r\n', '');
    if(!nickname) {//sending msg is username
        if(users[data]) {
            conn.write('\033[93m > nickname already in use. Try again:\033[39m ');
            return;
        } else {
            nickname = data;
            users[nickname] = conn;

            for(var i in users) {
                users[i].write('\033[90m > ' + nickname + ' joined the room\033[39m\n');
            }
        }
    } else {//sending msg is normal chat message
        for(var i in users) {
            if(i != nickname) {
                users[i].write('\033[96m > ' + nickname + ':\033[39m ' + data + '\n');
            }
        }
    }
});
conn.on('close', function(){
    count--;
    delete users[nickname];
    //conn.write('\033[90 > ' + nickname + ' left the room\033[39m\n');
    Object.keys(users).forEach(function (user) {
    users[user].write(nickname + ' left the room');
  });
});
conn.on('error', function(){
    console.log('\033[96m error occur.  \033[39m');
}
);

});
server.listen(3000, function (){
    console.log('\033[96m   server listening on *:3000\033[39m');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top