npm-package-nodejs-utils-lda
Version:
Este projeto tem como fins de criar e abstrair módulos basicos e utilidades para o node js
36 lines (32 loc) • 825 B
JavaScript
function broadcast(clientes, msg) {
console.log(`WSChat [broadcast] Send message!`);
for (const cliente of clientes) {
if (cliente.readyState === 1) {
// WebSocket.OPEN === 1
cliente.send(msg);
}
}
}
function arp(clientes) {
const nomes = [];
for (const c of clientes) {
if (c.name) nomes.push(c.name);
}
return nomes;
}
function sendTo(clientes, targetName, msg) {
for (const cliente of clientes) {
if (cliente.name === targetName && cliente.readyState === WebSocket.OPEN) {
cliente.send(msg);
console.log(`WSChat [direct] Sent to ${targetName}!}`);
return true;
}
}
console.log(`WSChat [direct] Client ${targetName} not found or disconnected`);
return false;
}
module.exports = {
broadcast,
arp,
sendTo,
};