使用 socket.io 创建已连接客户端列表 [英] Create a list of Connected Clients using socket.io

查看:98
本文介绍了使用 socket.io 创建已连接客户端列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有 2 个相关问题.将它们放在一起更有意义.

Here are 2 related questions. Posting them together makes more sense.

问题 1

我有一个 node.js 应用程序,它向所有客户端发出一个事件,所有当前客户端都将响应 ready 发出.如何创建所有回复初始发出的客户端的列表,以及可以使用什么样的标识来区分客户端?

I have a node.js app which emits an event out to all clients, and all current clients will respond with a ready emit. How can I create a list of all the clients that replied to the initial emit, and what kind of identification can be used to distinguish the clients?

问题 2:

在收集连接的客户端列表后,我想要做的是访问 N 行数的 MySQL 数据库表并分配每个客户端 X 行每个.这些行将被发送回各自的客户端.这怎么办?

What I am trying to do after collect a list of connected clients, is to then access a MySQL database table of N number of rows and assign each client X rows each. These rows will be emitted back to their respective clients. How can this be done?

Qn 1 的当前代码

节点代码

setInterval(function() {
    util.log('Checking for new jobs...');
    dbCheckQueue(function(results) {  // checks if there are new rows to "distribute" to clients
        if (results.length) {
            util.log(results.length + ' new jobs found.');
            io.sockets.emit('job_available');
        }
    });
}, 10*1000);

客户端JS代码

socket.on('job_available', function() {
                console.log('Job Available.. Responding with Ready!');
                socket.emit('ready');
            });

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        getListings(client_id, function(listings) {
            socket.emit('job', listings);   // send jobs
        });
    });
});

Qn 2 的当前代码该代码适用于单个客户端,但如何遍历所有连接的客户端并执行相同的列更新和行选择?

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        getListings(client_id, function(listings) {
            socket.emit('job', listings);   // send jobs
        });
    });
});

推荐答案

您需要自己跟踪连接的客户端.这样做的简单方法是使用数组:

You will need to keep track of the connected clients yourself. The simple way to do that would be to use an array:

var clients = [];

io.sockets.on('connect', function(client) {
    clients.push(client); 

    client.on('disconnect', function() {
        clients.splice(clients.indexOf(client), 1);
    });
});

然后你可以在你需要的任何地方,在你的 ready 事件处理程序或其他任何地方引用服务器上的 clients 数组.类似的东西:

Then you can references that clients array on the server wherever you need to, in your ready event handler or whatever. Something like:

io.sockets.on('connection', function(socket) {
    socket.on('ready', function() {
        // UPDATE N rows with client_id in column checkout.
        // Then SELECTS * from table where checkout = client_id
        clients.forEach(function(client, index) {
            var client_id = index; // Just use the index in the clients array for now
            getListings(client_id, function(listings) {
                socket.emit('job', listings);   // send jobs
            });
        });
    });
});

这篇关于使用 socket.io 创建已连接客户端列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆