mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-22 22:36:06 +08:00
35 lines
774 B
TypeScript
35 lines
774 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));
|
|
});
|
|
}
|
|
}
|