【问题标题】:Socket.io with Node Cluster Module带有节点集群模块的 Socket.io
【发布时间】:2018-02-27 15:09:58
【问题描述】:


我正在寻找使用 Node 集群模块和 socket.io 运行 Node.js 应用程序。我使用粘性会话进行了设置,它可以工作,但我的问题如下:
例如,如果我连接到工人 5。
例如,另一个人连接到工人 4。
当我发送消息时,只有同一个工作人员的其他人会收到消息,但我希望如果我在 1 个工作人员上发送消息,它也会发送给其他工作人员。

这是我的服务器代码。

var sticky = require('sticky-session'),
    http = require('http'),
    express = require('express'),
    socketIO = require('socket.io'),
    cluster = require('cluster'),
    port = process.env.PORT || 3003;

  var app = express(), io;

  server = http.Server(app);

  app.get('/', function(req, res) {
    res.sendfile('index.html');
  });

  io = socketIO(server);
  let totalUsers = 0;

  io.on('connection', function(socket) {
    socket.on('chat message', function(msg) {
      console.log("got request");
      io.emit('chat message', msg+" send by worker "+cluster.worker.id);
    });
  });

if(!sticky.listen(server,port))
{
  server.once('listening', function() {
    console.log('Server started on port '+port);
  });

  if (cluster.isMaster) {
    console.log('Master server started on port '+port);
  }
}
else {
    console.log('- Child server started on port '+port+' case worker id='+cluster.worker.id);
}

这是我的客户代码

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
      #messages { margin-bottom: 40px }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      $(function () {
        var socket = io();
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });
        socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text(msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
      });
    </script>
  </body>
</html>

【问题讨论】:

    标签: javascript node.js socket.io


    【解决方案1】:

    socket.emit('聊天消息', $('#m').val());相反,您想向所有套接字发出使用此 io.socket.emit('chat message',$('#m').val());或者使用 socket.broadcast.emit()

    【讨论】:

    • 我认为这不是 OP 所要求的。这并不能解决 socket.io 集群实现的任何问题。
    【解决方案2】:

    socket.io 本身不支持集群。要获得适当的集群支持,以便您可以向所有集群上的所有用户或任何单个用户发出,无论他们在哪个集群上,您需要使用像 socket.io redis 适配器这样的东西来记录所有用户所在的集群所有集群都可以访问的redis内存数据库。

    粘性会话将给定的客户端保持在同一个集群上,但它们不能解决与不同集群上的套接字通信相关的任何问题。这就是 redis 适配器为你做的事情。

    在此处的 socket.io 文档中描述了配置该 redis 适配器:https://socket.io/docs/using-multiple-nodes/

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 2015-06-25
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多