50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2024-2025 Тарабанов Александр Викторович
|
|
* All rights reserved.
|
|
*
|
|
* This software is proprietary and confidential.
|
|
* Unauthorized copying, modification, or distribution is prohibited.
|
|
*
|
|
* For licensing inquiries: info@hb3-accelerator.com
|
|
* Website: https://hb3-accelerator.com
|
|
* GitHub: https://github.com/HB3-ACCELERATOR
|
|
*/
|
|
|
|
const WebSocket = require('ws');
|
|
|
|
let wss = null;
|
|
const wsClients = new Set();
|
|
|
|
function initWSS(server) {
|
|
wss = new WebSocket.Server({ server, path: '/ws' });
|
|
wss.on('connection', (ws) => {
|
|
wsClients.add(ws);
|
|
ws.on('close', () => wsClients.delete(ws));
|
|
});
|
|
}
|
|
|
|
function broadcastContactsUpdate() {
|
|
for (const ws of wsClients) {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.send(JSON.stringify({ type: 'contacts-updated' }));
|
|
}
|
|
}
|
|
}
|
|
|
|
function broadcastMessagesUpdate() {
|
|
for (const ws of wsClients) {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.send(JSON.stringify({ type: 'messages-updated' }));
|
|
}
|
|
}
|
|
}
|
|
|
|
function broadcastChatMessage(message) {
|
|
for (const ws of wsClients) {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.send(JSON.stringify({ type: 'chat-message', message }));
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { initWSS, broadcastContactsUpdate, broadcastMessagesUpdate, broadcastChatMessage };
|