2024-07-02 00:30:10 -04:00
|
|
|
import { MSAgentAddUserMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
|
2024-07-01 23:34:28 -04:00
|
|
|
import { Client } from "./client.js";
|
|
|
|
|
|
|
|
export class MSAgentChatRoom {
|
|
|
|
clients: Client[];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.clients = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
addClient(client: Client) {
|
|
|
|
this.clients.push(client);
|
|
|
|
client.on('close', () => {
|
|
|
|
this.clients.splice(this.clients.indexOf(client), 1);
|
2024-07-02 00:30:10 -04:00
|
|
|
if (client.username === null) return;
|
|
|
|
let msg: MSAgentRemoveUserMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.RemoveUser,
|
|
|
|
data: {
|
|
|
|
username: client.username
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const _client of this.getActiveClients()) {
|
|
|
|
_client.send(msg);
|
|
|
|
}
|
2024-07-01 23:34:28 -04:00
|
|
|
});
|
2024-07-02 00:30:10 -04:00
|
|
|
client.on('join', () => {
|
|
|
|
client.send(this.getInitMsg());
|
|
|
|
let msg: MSAgentAddUserMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.AddUser,
|
|
|
|
data: {
|
|
|
|
username: client.username!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const _client of this.getActiveClients().filter(c => c !== client)) {
|
|
|
|
_client.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private getInitMsg(): MSAgentInitMessage {
|
|
|
|
return {
|
|
|
|
op: MSAgentProtocolMessageType.Init,
|
|
|
|
data: {
|
|
|
|
users: this.clients.filter(c => c.username !== null).map(c => c.username!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getActiveClients() {
|
|
|
|
return this.clients.filter(c => c.username !== null);
|
2024-07-01 23:34:28 -04:00
|
|
|
}
|
|
|
|
}
|