문제

I'm trying to set socket parameter (nick) as follows

socket.on('something', function(who){
    socket.nick = who;
    //something
});

Following code returns undefined to the client, which is the issue here.

But when i do it like...

socket.on('something', function(who){
    socket.nick = 'who';
    //something
});

This returns 'who' to everyone, ofc.

Is there a way to get the upper code snippet working?

도움이 되었습니까?

해결책

Sockets can't communicate JavaScript objects or anything that can't be stringified simply. If who is an object as in {x: "monkey"}, you probably want to use who.x instead, which is a string:

socket.nick = who.x;

You could also stringify the object to JSON and then parse it on the client:

socket.nickJson = JSON.stringify(who);

다른 팁

With the lightning fast aid of Explosion pills the problem got solved.

instead of...

socket.on('something', function(who){
    socket.nick = who;
    //something
});

it should be

socket.on('something', function(who){
    socket.nick = who.x;
    //something
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top