I have a clientid and username and i want them both send with the socket.
我有一个clientid和用户名,我想让他们都用套接字发送。
client.userid = userid;
client.username = username;
client.emit('onconnected', { id: client.userid, name: client.username });
i tried this for example but it doesn't seem to work
我试过这个,但它似乎不起作用。
2 个解决方案
#1
15
You can try this
你可以试试这个
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 });
}
}
and on the client
和在客户端
socket.on('eventToClient',function(data) {
// do something with data
var id = data.id
var name = data.username
});
#2
6
Try sending the object as a whole:
尝试将对象作为一个整体发送:
$('form').submit(function(){
var loginDetails={
userid : userid,
username : username
};
client.emit('sentMsg',loginDetails);
}
#1
15
You can try this
你可以试试这个
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 });
}
}
and on the client
和在客户端
socket.on('eventToClient',function(data) {
// do something with data
var id = data.id
var name = data.username
});
#2
6
Try sending the object as a whole:
尝试将对象作为一个整体发送:
$('form').submit(function(){
var loginDetails={
userid : userid,
username : username
};
client.emit('sentMsg',loginDetails);
}