mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 06:46:09 +08:00
34 lines
708 B
TypeScript
34 lines
708 B
TypeScript
import { Service, Inject } from 'typedi';
|
|
import winston from 'winston';
|
|
import { Connection } from 'sockjs';
|
|
|
|
@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: string) {
|
|
this.clients.forEach((x) => {
|
|
x.write(msg);
|
|
});
|
|
}
|
|
}
|