Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stream_client_server.clients.forEach((client) => {
if (client.readyState !== WebSocket.OPEN) {
return;
}
// If this client's stream id and token match, broadcast.
if (client.stream_id === settings.stream_id && client.stream_token === settings.stream_token) {
client.send(data);
}
});
};
const send = () => {
if (sock.readyState === WebSocket.OPEN) {
// Send at least one payload every 5 seconds
setTimeout(() => {
const data = {
datetime: Date.now(),
department: 'Sales',
itemId: Math.round(Math.random() * 20000),
qty: Math.round(Math.random() * 1000),
price: (Math.random() * 50).toFixed(2)
}
log.info('sending mock data to client: %j', data)
sock.send(JSON.stringify(data))
// Queue another send
send()
function exec(ws) {
if (ws.readyState === WebSocket.CLOSE) return
if (state.buff.length > 0 && ws.readyState === WebSocket.OPEN) {
state.websocketTransmitDelayMillis = 10
return ws.send(JSON.stringify(state.buff.shift()), () => exec(ws))
}
// some sort of exponential wait time with an upper cap
// 10 * 2^8
if (state.websocketTransmitDelayMillis < 2560) {
state.websocketTransmitDelayMillis *= 2
}
setTimeout(() => exec(ws), state.websocketTransmitDelayMillis)
}
this.wssOutgoing.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
try {
client.send(data, this.onBroadcastError);
} catch (err) {
this.onBroadcastError(err);
} finally {
console.log('Something broke. :(');
}
}
});
}
webSock.prototype.send = function(node, obj) {
if (this.socket) {
if (this.socket.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify(obj, null, 2), error => {
if (error) node.warn(`Websocket send error: ${error}`);
});
} else {
node.warn(`web socket not open when sending '${JSON.stringify(obj, null, 2)}'`);
}
}
};
sendToMiner(payload: CoinHiveResponse) {
const coinhiveMessage = JSON.stringify(payload);
if (this.online && this.ws.readyState === WebSocket.OPEN) {
try {
this.ws.send(coinhiveMessage);
} catch (e) {
this.kill();
}
}
}
constructor (socket) {
super();
this.callback_queue = new Map();
this.callback_ids = 0;
this.socket = socket;
this.socket.on('message', this.handleMessage.bind(this));
this.socket.on('open', this.handleOpen.bind(this));
this.socket.on('close', this.handleClose.bind(this));
this.connected = this.socket.readyState === WebSocket.OPEN;
}
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
if (client.page === "status") {
client.send(JSON.stringify({id: appID, data: data}));
}
}
});
},
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
};