[英]How to send to a client individually using socket_id?
在客户中:
socket.on("event_test", function(msg){ console.log(msg);
alert(msg);
});
function test()
{
socket.emit("hello","my_username");
}
在服务器中:
socket.on("hello", function(new_username){
socket.to(socket.id).emit("event_test", "pobe" );
console.log(socket.id); // socket id is being printed correctly
});
我编写此代码只是为了检查.to()功能。 普通的socket.emit可以正常工作。 仅当我尝试将其发送到特定客户端时,它不会发出并且我无法在客户端中接收到它。 我究竟做错了什么 ? 跟着这个: https : //socket.io/docs/emit-cheatsheet/
socket.on("hello", function(id, new_username){
socket.broadcast.to(id).emit("event_test", "pobe" );
});
查看您的备忘单,您看起来很正确,我不是专家,但我认为您可能已经从服务器向自己发送了邮件。
编辑:
如果这不起作用,请尝试:
socket.on("hello", function(new_username){
socket.emit("event_test", "pobe" );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.