qinglong/back/services/sock.ts
copilot-swe-agent[bot] 013f44b2bd Address code review feedback - extract constants and utility functions
Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-07 16:25:04 +00:00

35 lines
773 B
TypeScript

import { Service, Inject } from 'typedi';
import winston from 'winston';
import { Connection } from 'sockjs';
import { SockMessage } from '../data/sock';
@Service()
export default class SockService {
private clients: Connection[] = [];
constructor(@Inject('logger') private logger: winston.Logger) {}
public getClients() {
return this.clients;
}
public addClient(conn: Connection) {
if (this.clients.indexOf(conn) === -1) {
this.clients.push(conn);
}
}
public removeClient(conn: Connection) {
const index = this.clients.indexOf(conn);
if (index !== -1) {
this.clients.splice(index, 1);
}
}
public sendMessage(msg: SockMessage) {
this.clients.forEach((x) => {
x.write(JSON.stringify(msg));
});
}
}