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

View file

@ -1,13 +1,16 @@
import EventEmitter from "events"; import EventEmitter from "events";
import { WebSocket } from "ws"; 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 { export class Client extends EventEmitter {
username: string | null; username: string | null;
room: MSAgentChatRoom;
socket: WebSocket; socket: WebSocket;
constructor(socket: WebSocket) { constructor(socket: WebSocket, room: MSAgentChatRoom) {
super(); super();
this.socket = socket; this.socket = socket;
this.room = room;
this.username = null; this.username = null;
this.socket.on('message', (msg, isBinary) => { this.socket.on('message', (msg, isBinary) => {
if (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) { private parseMessage(data: string) {
let msg: MSAgentProtocolMessage; 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 Fastify from 'fastify';
import FastifyWS from '@fastify/websocket'; import FastifyWS from '@fastify/websocket';
import { Client } from './client.js'; import { Client } from './client.js';
import { MSAgentChatRoom } from './room.js';
const app = Fastify({ const app = Fastify({
logger: true, logger: true,
@ -8,8 +9,11 @@ const app = Fastify({
app.register(FastifyWS); app.register(FastifyWS);
let room = new MSAgentChatRoom();
app.get("/socket", {websocket: true}, (socket, req) => { app.get("/socket", {websocket: true}, (socket, req) => {
let client = new Client(socket); let client = new Client(socket, room);
room.addClient(client);
}); });
let port; let port;

View file

@ -1,3 +1,4 @@
import { MSAgentAddUserMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
import { Client } from "./client.js"; import { Client } from "./client.js";
export class MSAgentChatRoom { export class MSAgentChatRoom {
@ -11,6 +12,41 @@ export class MSAgentChatRoom {
this.clients.push(client); this.clients.push(client);
client.on('close', () => { client.on('close', () => {
this.clients.splice(this.clients.indexOf(client), 1); 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);
}
} }