Estoy usando Socket.io en un servidor Node.js.
for (connectedSocket of io.sockets.sockets) { console.log(`TEST: id = ${connectedSocket.id}`) //Why is this 'undefined'? All I want is the unique identifier of each socket in the server. }No estoy seguro de cómo solucionar esto, pero tiene que ser súper simple. Perdón por el formato del código fallido :o
A partir de socket.io v3, io.sockets ahora es un objeto Map como se muestra aquí . Puedes iterarlo directamente con:
for (let [id, socket] of io.sockets.entries()) { // can use socket here }Puedes usar la nueva interfaz:
let socketsArray = await io.fetchSockets();para obtener una variedad de enchufes conectados.
Tenga en cuenta que también puede usar fetchSockets() con habitaciones y espacios de nombres como se muestra aquí .