Answer the question
In order to leave comments, you need to log in
How to get data by key wss.clients without enumeration of all users?
Hello. We are talking about the server (WebSocket).
In wss.clients all users, in order to get information about a specific user, you have to iterate and compare all of them:
const WebSocket = require('ws');
const wss = new WebSocket.server({ port:8080 });
...
wss.clients.forEach(function each(client) {
if(id === client.id) {
console.log('ID: ' + client.id)
console.log('KEY:' + client. key)
}
})
Answer the question
In order to leave comments, you need to log in
You can create your own object for storing users and save the Id there when connecting, then you can refer to specific users
myClients = [];
wss.on('connection', function(ws) {
myClients.push(ws);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question