Zoltán Kiss
Zoltán Kiss

Reputation: 41

Node.js Socket.io, socket.emit doesn't work

I'm pretty new here. I'm trying to use the emit function in this case with no luck :(.

Server Side:

socket.on('newUser', function(msg){
    ConnectedSockets ++;
    var player;
    player = new Player();

    player.CreatePlayer(msg.id, msg.name, msg.status);
        socket.id=msg.id;
        socket.name=msg.name;
        sockets[socket.id] = socket;

        for(i in sockets){
            var myVar=sockets[i].name;
            socket.emit('listing',{name: myVar} );
        }
}) ;

Client Side:

socket.on('listing', function(msg){ 
    $('#list').val($('#list').val() + "\n"+msg.name);
}) ;    

When I send newUser event on node.js console, no messages appear in response. If I write sys.puts("socket name:"+ myVar) in the for cycle, all the socket names appear as I expected.

Why the client doesn't receive any answer (no error message or any kind of warning)? :)

Upvotes: 2

Views: 1048

Answers (1)

Zoltán Kiss
Zoltán Kiss

Reputation: 41

Problematic line:

socket.id=msg.id;

In Socket.io socket.id has a predefined value, and it must not be changed. That caused the error.

Upvotes: 2

Related Questions