【问题标题】:Socket.io + Redis - clients are joining each others' "private" roomsSocket.io + Redis - 客户端正在加入彼此的“私人”房间
【发布时间】:2018-12-01 04:04:26
【问题描述】:

我刚刚开始使用 Socket.io 和 Redis 进行发布/订阅消息传递,这非常棒。我的应用程序的一个重要特性是服务器需要能够向房间的所有订阅者广播消息,并且还需要在该房间中选择 1 个订阅者并将消息窄播给他们。目前,该订阅者是随机选择的。基于阅读socket.io的文档,我认为我可以做到这一点。

但是,我遇到了一些我不明白的事情。在 Socket.io 的 Default Room 文档 (https://socket.io/docs/rooms-and-namespaces/#default-room) 中,他们说每个套接字会自动加入一个以其套接字 ID 命名的房间。这看起来可以解决我的窄播要求——查看连接到我的“大”房间的客户端 ID 列表,随机选择一个,然后向房间发送一条与所选 ID 同名的消息。

但是,它不起作用,因为出于某种原因,所有客户端都加入了彼此的默认房间。我在我的代码中没有看到任何异常,但我的“窄播”消息将发送给所有客户端。

这是我的服务器端代码:

var io = require('socket.io');
var redisAdapter = require('socket.io-redis');
var server = io();

server.adapter(redisAdapter({ host: 'localhost', port: 6379 }))

server.on('connect', (socket) => {
  console.log(`${socket.id} connected!`);
  socket.join('new');
  server.emit('welcome', `Please give a warm welcome to ${socket.id}!`);
  server.to(socket.id).emit('private', 'Just between you and me, I think you are going to like it here');
});

server.listen(3000);

setInterval(whisperAtRandom, 2000);

function whisperAtRandom() {
  server.in('new').adapter.clients((err, clients) => {
    if (err) throw err;

    console.log('Clients on channel "new": ', clients);
    chosenOne = clients[Math.floor(Math.random()*clients.length)];
    console.log(`Whispering to ${chosenOne}`);
    server.to(chosenOne).emit('private', { message: `Psssst... hey there ${chosenOne}`});
    server.in(chosenOne).adapter.clients((err, clients) => {
      console.log(`Clients in ${chosenOne}: ${clients}`)
    })
  });
}

它启动服务器,监听 3000 端口,然后每 2 秒向“新”房间中的随机客户端发送一条消息。它还会注销连接到“新”房间和“”房间的客户端列表。

这是客户端代码:

var sock = require('socket.io-client')('http://localhost:3000');

sock.connect();

sock.on('update', (data) => {
  console.log('Updating...');
  console.log(data);
});

sock.on('new', (data) => {
  console.log(`${sock.id} accepting request for new`);
  console.log(data.message);
});

sock.on('welcome', (data) => {
  console.log('A new challenger enters the ring');
  console.log(data);
});

sock.on('private', (data) => {
  console.log(`A private message for me, ${sock.id}???`);
  console.log(data);
});

我的问题是我所有的客户都连接到彼此的“”房间。这是我的日志中的一个示例:

0|socket-s | Clients on channel "new":  [ 'dJaoZd6amTfdQy5NAAAA', 'bwG1yTT46dr5R_G6AAAB' ]
0|socket-s | Whispering to bwG1yTT46dr5R_G6AAAB
2|socket-c | A private message for me, dJaoZd6amTfdQy5NAAAA???
2|socket-c | Psssst... hey there bwG1yTT46dr5R_G6AAAB
1|socket-c | A private message for me, bwG1yTT46dr5R_G6AAAB???
1|socket-c | Psssst... hey there bwG1yTT46dr5R_G6AAAB
0|socket-s | Clients in bwG1yTT46dr5R_G6AAAB: dJaoZd6amTfdQy5NAAAA,bwG1yTT46dr5R_G6AAAB

您可以看到dJaoZ...bwG1y... 这两个客户端都收到了“私人”消息,并且两个客户端都连接到bwG1y... 的默认房间。

这是为什么?这与我的两个客户端都在同一台机器上运行(具有不同的节点进程)这一事实有关吗?我在 Socket.io 的文档中遗漏了什么吗?任何帮助表示赞赏!

PS——更令人困惑的是,server.on('connect', ... 中出现的私人消息有效!每个客户端在连接到服务器后就收到一次“Just between you and me ...”消息。

【问题讨论】:

    标签: node.js redis socket.io


    【解决方案1】:

    你的主要问题可能是由whisperAtRandom函数中的server.to引起的,私信使用socket而不是服务器:

    socket.to(chosenOne).emit('private', { message: `Psssst... hey there ${chosenOne}`});
    

    要回答您的其他问题,请尝试更改这些:

    server.emit('welcome', `Please give a warm welcome to ${socket.id}!`);
    server.to(socket.id).emit('private', 'Just between you and me, I think you are going to like it here');
    

    收件人:

    socket.broadcast.emit('welcome', `Please give a warm welcome to ${socket.id}!`);
    socket.emit('private', 'Just between you and me, I think you are going to like it here');
    

    查看我的 Socket.IO 备忘单:

    // Socket.IO Cheatsheet
    
    // Add socket to room
    socket.join('some room');
    
    // Remove socket from room
    socket.leave('some room');
    
    // Send to current client
    socket.emit('message', 'this is a test');
    
    // Send to all clients include sender
    io.sockets.emit('message', 'this is a test');
    
    // Send to all clients except sender
    socket.broadcast.emit('message', 'this is a test');
    
    // Send to all clients in 'game' room(channel) except sender
    socket.broadcast.to('game').emit('message', 'this is a test');
    
    // Send to all clients in 'game' room(channel) include sender
    io.sockets.in('game').emit('message', 'this is a test');
    
    // sending to individual socket id
    socket.to(socketId).emit('hey', 'I just met you');
    

    【讨论】:

    • 您的编辑对于解决此问题的其他方法很有启发性,但不完全是我想要的。我更关心whisperAtRandom 函数中的emit,而不是server.on('connect'... 代码中的emit。基本上,为什么所有客户都出现在每个客户的默认房间中?还有,很棒的备忘单!我一定会拯救它。但是,对我来说最有用的最后一行不再起作用了。虽然很近。看到这个错误:github.com/socketio/socket.io/issues/1618
    • 我已经更新了答案,也许这导致了你的问题。
    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 2014-08-26
    • 2015-08-17
    • 2017-05-22
    • 1970-01-01
    • 2011-11-18
    • 2018-01-09
    • 1970-01-01
    相关资源
    最近更新 更多