on the server side i got
在服务器端我得到了
socket.on('chat', function (data) {
io.sockets.socket(data.clientid).emit('chat', {
msg: data.msg,
senderid : senderid
});
});
How can I get the get the senderid without the sender having to post their clientid through with the message?
如果没有发件人必须通过邮件发布他们的clientid,我怎样才能得到发件人?
2 个解决方案
#1
21
It turns out I can just use socket.id of the user sending through a msg to get their clientid, such as:
事实证明我可以使用用户通过msg发送的socket.id获取他们的clientid,例如:
socket.on('chat', function (data) {
io.sockets.socket(data.clientid).emit('chat', {
msg: data.msg,
senderid : socket.id
});
});
Originally I thought I could just fetch the clientid by doing :
最初我以为我可以通过这样做来获取clientid:
io.sockets.on('connection', function (socket) {
clientid = socket.id;
But clientid will be the last person who connected in this instance, not the last person who sent through a chat
但是clientid将是最后一个在这种情况下连接的人,而不是通过聊天发送的最后一个人
#2
0
I'd suggest sending over a username (make sure it's unique) and keeping an object of client ids associated with usernames in memory.
我建议发送一个用户名(确保它是唯一的)并保留一个客户ID的对象与内存中的用户名相关联。
#1
21
It turns out I can just use socket.id of the user sending through a msg to get their clientid, such as:
事实证明我可以使用用户通过msg发送的socket.id获取他们的clientid,例如:
socket.on('chat', function (data) {
io.sockets.socket(data.clientid).emit('chat', {
msg: data.msg,
senderid : socket.id
});
});
Originally I thought I could just fetch the clientid by doing :
最初我以为我可以通过这样做来获取clientid:
io.sockets.on('connection', function (socket) {
clientid = socket.id;
But clientid will be the last person who connected in this instance, not the last person who sent through a chat
但是clientid将是最后一个在这种情况下连接的人,而不是通过聊天发送的最后一个人
#2
0
I'd suggest sending over a username (make sure it's unique) and keeping an object of client ids associated with usernames in memory.
我建议发送一个用户名(确保它是唯一的)并保留一个客户ID的对象与内存中的用户名相关联。