finishing touches
This commit is contained in:
14
dist/ws.js
vendored
14
dist/ws.js
vendored
@@ -1,27 +1,13 @@
|
||||
import { logInfo } from "./logger.js";
|
||||
export class WsService {
|
||||
clients = new Set();
|
||||
listeners = [];
|
||||
onClientChange(callback) {
|
||||
this.listeners.push(callback);
|
||||
return () => {
|
||||
this.listeners = this.listeners.filter((cb) => cb !== callback);
|
||||
};
|
||||
}
|
||||
emitClientChange() {
|
||||
for (const callback of this.listeners) {
|
||||
callback(this.clients.size);
|
||||
}
|
||||
}
|
||||
addClient(ws) {
|
||||
this.clients.add(ws);
|
||||
logInfo(`Socket connected. Total clients: ${this.clients.size}`);
|
||||
this.emitClientChange();
|
||||
}
|
||||
removeClient(ws) {
|
||||
this.clients.delete(ws);
|
||||
logInfo(`Socket disconnected. Total clients: ${this.clients.size}`);
|
||||
this.emitClientChange();
|
||||
}
|
||||
broadcast(message) {
|
||||
this.clients.forEach((socket) => {
|
||||
|
||||
Reference in New Issue
Block a user