io.sockets.on(‘connection’, function(socket) { socket.object = socket.id;
socket.on('updateObject', function(data) { // How to update socket.object here for all clients? }); });
怎么做?
请注意,此功能在高于1.0的socket.io版本中不再可用,建议保留您的socket.id数组,以便您可以在需要时对其进行迭代。
您可以使用以下forEach功能实现此目的:
forEach
io.sockets.on('connection', function(socket) { socket.object = socket.id; socket.on('updateObject', function(data) { io.sockets.clients('room').forEach(function (socket, data) { // goes through all clients in room 'room' and lets you update their socket objects }); }); });