10
我有一個clientid和用戶名,我希望他們都用套接字發送。如何使用Socket.io在一條消息中發送兩個變量?
client.userid = userid;
client.username = username;
client.emit('onconnected', { id: client.userid, name: client.username });
我想這例如但它似乎沒有工作
我有一個clientid和用戶名,我希望他們都用套接字發送。如何使用Socket.io在一條消息中發送兩個變量?
client.userid = userid;
client.username = username;
client.emit('onconnected', { id: client.userid, name: client.username });
我想這例如但它似乎沒有工作
你可以試試這個
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 });
}
}
,並在客戶端上
socket.on('eventToClient',function(data) {
// do something with data
var id = data.id
var name = data.username
});
嘗試發送對象作爲一個整體:
$('form').submit(function(){
var loginDetails={
userid : userid,
username : username
};
client.emit('sentMsg',loginDetails);
}