implement basic commands

This commit is contained in:
Elijah R 2024-07-02 00:30:10 -04:00
parent 4862747935
commit 75607abfcf
4 changed files with 82 additions and 3 deletions

View file

@ -3,6 +3,7 @@ export enum MSAgentProtocolMessageType {
Join = "join",
Talk = "talk",
// Server-to-client
Init = "init",
AddUser = "adduser",
RemoveUser = "remuser",
Message = "msg"
@ -30,6 +31,13 @@ export interface MSAgentTalkMessage extends MSAgentProtocolMessage {
// Server-to-client
export interface MSAgentInitMessage extends MSAgentProtocolMessage {
op: MSAgentProtocolMessageType.Init,
data: {
users: string[]
}
}
export interface MSAgentAddUserMessage extends MSAgentProtocolMessage {
op: MSAgentProtocolMessageType.AddUser,
data: {

View file

@ -1,13 +1,16 @@
import EventEmitter from "events";
import { WebSocket } from "ws";
import { MSAgentProtocolMessage } from '@msagent-chat/protocol';
import { MSAgentProtocolMessage, MSAgentProtocolMessageType } from '@msagent-chat/protocol';
import { MSAgentChatRoom } from "./room.js";
export class Client extends EventEmitter {
username: string | null;
room: MSAgentChatRoom;
socket: WebSocket;
constructor(socket: WebSocket) {
constructor(socket: WebSocket, room: MSAgentChatRoom) {
super();
this.socket = socket;
this.room = room;
this.username = null;
this.socket.on('message', (msg, isBinary) => {
if (isBinary) {
@ -22,7 +25,35 @@ export class Client extends EventEmitter {
});
}
send(msg: MSAgentProtocolMessage) {
return new Promise<void>((res, rej) => {
this.socket.send(JSON.stringify(msg), err => {
if (err) {
rej(err);
return;
}
res();
});
});
}
private parseMessage(data: string) {
let msg: MSAgentProtocolMessage;
try {
msg = JSON.parse(data);
} catch {
this.socket.close();
return;
}
switch (msg.op) {
case MSAgentProtocolMessageType.Join: {
break;
}
case MSAgentProtocolMessageType.Talk: {
break;
}
}
}
}

View file

@ -1,6 +1,7 @@
import Fastify from 'fastify';
import FastifyWS from '@fastify/websocket';
import { Client } from './client.js';
import { MSAgentChatRoom } from './room.js';
const app = Fastify({
logger: true,
@ -8,8 +9,11 @@ const app = Fastify({
app.register(FastifyWS);
let room = new MSAgentChatRoom();
app.get("/socket", {websocket: true}, (socket, req) => {
let client = new Client(socket);
let client = new Client(socket, room);
room.addClient(client);
});
let port;

View file

@ -1,3 +1,4 @@
import { MSAgentAddUserMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
import { Client } from "./client.js";
export class MSAgentChatRoom {
@ -11,6 +12,41 @@ export class MSAgentChatRoom {
this.clients.push(client);
client.on('close', () => {
this.clients.splice(this.clients.indexOf(client), 1);
if (client.username === null) return;
let msg: MSAgentRemoveUserMessage = {
op: MSAgentProtocolMessageType.RemoveUser,
data: {
username: client.username
}
};
for (const _client of this.getActiveClients()) {
_client.send(msg);
}
});
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);
}
}