您将需要自己跟踪连接的客户端。做到这一点的简单方法是使用数组:
var clients = [];io.sockets.on('connect', function(client) { clients.push(client); client.on('disconnect', function() { clients.splice(clients.indexOf(client), 1); });});然后,您可以
clients在任何需要的位置,
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 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 }); }); });});


