2013-12-17 3 views
10

私はclientidとユーザー名を持っていて、両方ともソケットで送信します。 Socket.ioを使用して1つのメッセージで2つの変数を送信するにはどうすればよいですか?

client.userid = userid; 
client.username = username; 
client.emit('onconnected', { id: client.userid, name: client.username }); 

iは、例えば、これを試してみましたが、あなたがこの

io.sockets.on('connection', function (socket) { 
    socket.on('event_name', function(data) { 
     // you can try one of these three options 

     // this is used to send to all connecting sockets 
     io.sockets.emit('eventToClient', { id: userid, name: username }); 
     // this is used to send to all connecting sockets except the sending one 
     socket.broadcast.emit('eventToClient',{ id: userid, name: username }); 
     // this is used to the sending one 
     socket.emit('eventToClient',{ id: userid, name: username }); 
    } 
} 

を試すことができます

答えて

13

を動作するようには思えないし、クライアント上で

socket.on('eventToClient',function(data) { 
    // do something with data 
     var id = data.id 
     var name = data.username 

}); 
5

は、オブジェクトを送信してみてください全体として:

$('form').submit(function(){  
var loginDetails={ 
    userid : userid, 
    username : username 
    }; 
client.emit('sentMsg',loginDetails); 
} 
関連する問題