简体   繁体   English

Socket.io:如何检查是否有一定数量的客户端发出了事件?

[英]Socket.io: How can I check to see if a certain number of clients have emitted an event?

With Socket.io, I'm querying all connected clients to get a status on whether or not they are ready to do something. 使用Socket.io,我正在查询所有已连接的客户端,以获取有关它们是否准备做某事的状态。 Once they have all responded back, I want to emit an event. 他们都做出回应后,我想发出一个事件。

How can I check whether or not each client has responded, assuming that I already know how many clients are connected? 假设我已经知道连接了多少个客户端,如何检查每个客户端是否已响应?

Right now, the code looks like this: 现在,代码如下所示:

var totalClients = 2; //countClientSockets();
var readyClients = 0;

socket.on("syncReady", function() {
        debug.log("Client is ready!");
        readyClients++;
        debug.log("Ready Clients: " + readyClients + "/" + totalClients.length);
        if (readyClients == totalClients.length) {
            debug.log("Room is synced! Sending start event...");
            io.sockets.emit("play");
        } else {
            debug.log("Room is not synced yet! Waiting...");
        }
    });

However, this results in the console output (after both clients have gotten ready and responded): 但是,这会导致控制台输出(两个客户端都准备就绪并响应之后):

[Debug][Handler]: Client is ready!
[Debug][Handler]: Ready Clients: 1/2
[Debug][Handler]: Room is not synced yet! Waiting...
[Debug][Handler]: Client is ready!
[Debug][Handler]: Ready Clients: 1/2
[Debug][Handler]: Room is not synced yet! Waiting...

I would like it to output: 我希望它输出:

[Debug][Handler]: Client is ready!
[Debug][Handler]: Ready Clients: 1/2
[Debug][Handler]: Room is not synced yet! Waiting...
[Debug][Handler]: Client is ready!
[Debug][Handler]: Ready Clients: 2/2
[Debug][Handler]: Room is synced! Sending play event...

What would be a better way to do this? 有什么更好的方法可以做到这一点?

You definitely have the right idea and your code is close to correct. 您绝对有正确的主意,并且您的代码接近正确。

I'm guessing the main issue is that your readyClients is defined inside your io.on('connection',function(){}) handler (not totally sure though since that part of the code isn't shown). 我猜主要的问题是您的readyClients是在io.on('connection',function(){})处理函数中定义的(虽然由于未显示该部分代码,所以并不确定)。

You want to keep that variable outside of io.on so that it doesn't get recreated on every connection. 您想将该变量保留在io.on之外,以便不会在每个连接上都重新创建该变量。 I think this is what you are looking for: 我认为这是您要寻找的:

var totalClients = 2;
var readyClients = 0;

io.on('connection', function (socket) {
  socket.on("syncReady", function() {
    console.log("Client is ready!");
    readyClients++;
    console.log("Ready Clients: " + readyClients + "/" + totalClients);
    if (readyClients == totalClients) {
      console.log("Room is synced! Sending start event...");
      io.sockets.emit("play");
    } else {
      console.log("Room is not synced yet! Waiting...");
    }   
  }); 
});

I also changed totalClients.length to totalClients since totalClients isn't an array. 由于totalClients不是数组, totalClients我也将totalClients.length更改为totalClients

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM