Javascript 如何使用socket.io创建动态套接字室

Javascript 如何使用socket.io创建动态套接字室,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我想使用socket.io实现多个聊天室,我可以使用一个socket.room实现一对一聊天室,但我想创建多个socket聊天室来与多人并行聊天 下面是我在git中得到的示例,但我无法理解它如何适用于多个聊天,任何人都可以解释 服务器端 io = socketio.listen(server); // handle incoming connections from clients io.sockets.on('connection', function(socket) { // once a

我想使用socket.io实现多个聊天室,我可以使用一个socket.room实现一对一聊天室,但我想创建多个socket聊天室来与多人并行聊天

下面是我在git中得到的示例,但我无法理解它如何适用于多个聊天,任何人都可以解释
服务器端

io = socketio.listen(server);

// handle incoming connections from clients
io.sockets.on('connection', function(socket) {
// once a client has connected, we expect to get a ping from them 
saying what room they want to join
socket.on('room', function(room) {
    socket.join(room);
});
});

// now, it's easy to send a message to just the clients in a given 
room
room = "abc123";
io.sockets.in(room).emit('message', 'what is going on, party 
people?');

 // this message will NOT go to the client defined above
 io.sockets.in('foobar').emit('message', 'anyone in this room yet?');
  // set-up a connection between the client and the server
 var socket = io.connect();

 // let's assume that the client page, once rendered, knows what room 
 it wants to join
 var room = "abc123";

 socket.on('connect', function() {
 // Connected, let's sign-up for to receive messages for this room
 socket.emit('room', room);
  });

 socket.on('message', function(data) {
  console.log('Incoming message:', data);
 });
客户端

io = socketio.listen(server);

// handle incoming connections from clients
io.sockets.on('connection', function(socket) {
// once a client has connected, we expect to get a ping from them 
saying what room they want to join
socket.on('room', function(room) {
    socket.join(room);
});
});

// now, it's easy to send a message to just the clients in a given 
room
room = "abc123";
io.sockets.in(room).emit('message', 'what is going on, party 
people?');

 // this message will NOT go to the client defined above
 io.sockets.in('foobar').emit('message', 'anyone in this room yet?');
  // set-up a connection between the client and the server
 var socket = io.connect();

 // let's assume that the client page, once rendered, knows what room 
 it wants to join
 var room = "abc123";

 socket.on('connect', function() {
 // Connected, let's sign-up for to receive messages for this room
 socket.emit('room', room);
  });

 socket.on('message', function(data) {
  console.log('Incoming message:', data);
 });

假设一个用户有多个聊天室可供选择。当他点击一个特定的房间时,他会得到它的信息:在这个例子中是聊天室1。 客户端套接字(属于单击此文件室的用户)必须首先加入此文件室 → 这就是为什么我们有:

socket.emit(room, ChatRoom1) 
//在另一部分,服务器端将此客户端的套接字id添加到此文件室:

socket.on('room', function(room) {
    socket.join(room);
});
现在,如果要向属于特定房间的所有套接字发送消息,请在服务器部件上使用以下命令:

 io.sockets.in(ChatRoom1).emit('message', 'what is going on, party 
people?');
→ 事实上,这个命令只是向属于聊天室1的所有套接字发送一条消息

→ 基本上,一个房间就是一组socketId

因此,现在在客户端,您有:

 socket.on('message', function(data) {
  console.log('Incoming message:', data);
 });
这只是一个侦听器,您将在控制台日志中看到: 收到的信息:发生了什么事,聚会的人

当你很快进入聊天室时,你的插座就会加入聊天室,并监听每一个事件,直到你要求插座离开聊天室

所以现在你可以想象,在你的信息中,你有你的Id,你的房间Id和你的内容,当你发送它时,服务器会知道在哪里发送它

例如: 信息:{ 内容:“布拉布拉”, 用户:我, 日期:现在, 室友:聊天室1 }

在客户端,每次用户发送消息时: socket.emit('sendMessage',message)

在服务器端:

socket.on('sendMessage', function(message){
io.sockets.in(message.RoomId).emit('message', message);
})

您好,需要更多的信息,请您回复,请您详细介绍一下您不了解的内容,谢谢Consister X和Y,Z和D是4个用户,都是在线的。。。不,告诉我X是如何与Y,Z,D并行聊天的。。像FaceBook一样,我编辑了我的答案,抱歉耽搁了。之后你可以在网上找到大量的教程。