jQuery 如何使用 socket.io 向特定客户端发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17476294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a message to a particular client with socket.io
提问by Nizar B.
I'm starting with socket.io + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit()
function:- all the connected clients receive the same message.
我从 socket.io + node.js 开始,我知道如何在本地发送消息和广播socket.broadcast.emit()
功能:-所有连接的客户端都收到相同的消息。
Now, I would like to know how to send a private message to a particular client, I mean one socket for a private chat between 2 person (Client-To-Client stream). Thanks.
现在,我想知道如何向特定客户端发送私人消息,我的意思是一个用于 2 人(客户端到客户端流)之间私人聊天的套接字。谢谢。
回答by az7ar
You can use socket.io rooms. From the client side emit an event ("join" in this case, can be anything) with any unique identifier (email, id).
您可以使用 socket.io 房间。从客户端发出一个带有任何唯一标识符(电子邮件、ID)的事件(在这种情况下,“加入”可以是任何东西)。
Client Side:
客户端:
var socket = io.connect('http://localhost');
socket.emit('join', {email: [email protected]});
Now, from the server side use that information to create an unique room for that user
现在,从服务器端使用该信息为该用户创建一个独特的房间
Server Side:
服务器端:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('join', function (data) {
socket.join(data.email); // We are using room of socket io
});
});
So, now every user has joined a room named after user's email. So if you want to send a specific user a message you just have to
所以,现在每个用户都加入了一个以用户电子邮件命名的房间。因此,如果您想向特定用户发送消息,您只需要
Server Side:
服务器端:
io.sockets.in('[email protected]').emit('new_msg', {msg: 'hello'});
The last thing left to do on the client side is listen to the "new_msg" event.
在客户端要做的最后一件事是监听“new_msg”事件。
Client Side:
客户端:
socket.on("new_msg", function(data) {
alert(data.msg);
}
I hope you get the idea.
我希望你能明白。
回答by Adam
When a user connects, it should send a message to the server with a username which has to be unique, like an email.
当用户连接时,它应该使用必须唯一的用户名向服务器发送消息,例如电子邮件。
A pair of username and socket should be stored in an object like this:
一对用户名和套接字应该存储在这样的对象中:
var users = {
'[email protected]': [socket object],
'[email protected]': [socket object],
'[email protected]': [socket object]
}
On the client, emit an object to the server with the following data:
在客户端,使用以下数据向服务器发出一个对象:
{
to:[the other receiver's username as a string],
from:[the person who sent the message as string],
message:[the message to be sent as string]
}
On the server, listen for messages. When a message is received, emit the data to the receiver.
在服务器上,侦听消息。当收到消息时,将数据发送给接收者。
users[data.to].emit('receivedMessage', data)
On the client, listen for emits from the server called 'receivedMessage', and by reading the data you can handle who it came from and the message that was sent.
在客户端,侦听来自名为“receivedMessage”的服务器发出的信息,通过读取数据,您可以处理它来自谁以及发送的消息。
回答by Trojan
SURE:Simply,
当然:简单地说,
This is what you need :
这是你需要的:
io.to(socket.id).emit("event", data);
whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.
每当用户加入服务器时,都会生成套接字详细信息,包括 ID。这个 ID 真的有助于向特定的人发送消息。
first we need to store all the socket.ids in array,
首先我们需要将所有的 socket.ids 存储在数组中,
var people={};
people[name] = socket.id;
here name is the receiver name. Example:
这里的名字是接收者的名字。例子:
people["ccccc"]=2387423cjhgfwerwer23;
So, now we can get that socket.id with the receiver name whenever we are sending message:
所以,现在我们可以在发送消息时获取带有接收者名称的 socket.id:
for this we need to know the receivername. You need to emit receiver name to the server.
为此,我们需要知道接收者名称。您需要将接收器名称发送到服务器。
final thing is:
最后的事情是:
socket.on('chat message', function(data){
io.to(people[data.receiver]).emit('chat message', data.msg);
});
Hope this works well for you.
希望这对你有用。
Good Luck!!
祝你好运!!
回答by Vladimir Kurijov
You can refer to socket.io rooms. When you handshaked socket - you can join him to named room, for instance "user.#{userid}".
您可以参考 socket.io房间。当您握手套接字时 - 您可以将他加入命名房间,例如“user.#{userid}”。
After that, you can send private message to any client by convenient name, for instance:
之后,您可以通过方便的名称向任何客户端发送私人消息,例如:
io.sockets.in('user.125').emit('new_message', {text: "Hello world"})
io.sockets.in('user.125').emit('new_message', {text: "Hello world"})
In operation above we send "new_message" to user "125".
在上面的操作中,我们向用户“125”发送“new_message”。
thanks.
谢谢。
回答by Rami
In a project of our company we are using "rooms" approach and it's name is a combination of user ids of all users in a conversation as a unique identifier (our implementation is more like facebook messenger), example:
在我们公司的一个项目中,我们使用“房间”方法,它的名称是对话中所有用户的用户 ID 的组合作为唯一标识符(我们的实现更像是 Facebook 信使),例如:
|id | name |1 | Scott |2 | Susan
|身 | 姓名 |1 | 斯科特 |2 | 苏珊
"room" name will be "1-2" (ids are ordered Asc.) and on disconnect socket.io automatically cleans up the room
“房间”名称将是“1-2”(ID 以升序排列)并且在断开连接时 socket.io 会自动清理房间
this way you send messages just to that room and only to online (connected) users (less packages sent throughout the server).
通过这种方式,您只向那个房间发送消息,并且只向在线(连接)用户发送消息(整个服务器发送的包较少)。
回答by Lalit Mohan
Let me make it simpler with socket.io rooms. request a server with a unique identifier to join a server. here we are using an email as a unique identifier.
让我用 socket.io 房间让它更简单。请求具有唯一标识符的服务器加入服务器。在这里,我们使用电子邮件作为唯一标识符。
Client Socket.io
客户端Socket.io
socket.on('connect', function () {
socket.emit('join', {email: [email protected]});
});
When the user joined a server, create a room for that user
当用户加入服务器时,为该用户创建一个房间
Server Socket.io
服务器Socket.io
io.on('connection', function (socket) {
socket.on('join', function (data) {
socket.join(data.email);
});
});
Now we are all set with joining. let emit something to from the server to
room, so that user can listen.
现在我们都准备好加入了。让从服务器to
机房发出一些东西,以便用户可以收听。
Server Socket.io
服务器Socket.io
io.to('[email protected]').emit('message', {msg: 'hello world.'});
Let finalize the topic with listening to message
event to the client side
让我们通过收听message
客户端的事件来完成主题
socket.on("message", function(data) {
alert(data.msg);
});
The reference from Socket.io rooms
来自Socket.io 房间的参考