From d99523d94f091b6ec1e272bdd476db8a420f4866 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sun, 10 Sep 2023 06:36:41 -0400 Subject: [PATCH] Implement chat and fix duplicate message bug --- src/channel/Channel.ts | 43 +++++++++++++++++++------- src/ws/events/user/handlers/a.ts | 12 +++++++ src/ws/events/user/handlers/ch.ts | 1 - src/ws/events/user/handlers/devices.ts | 1 - src/ws/events/user/handlers/hi.ts | 1 - src/ws/events/user/handlers/m.ts | 1 - src/ws/events/user/index.ts | 2 ++ src/ws/message.ts | 1 + 8 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src/ws/events/user/handlers/a.ts diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index 574f83e..9a4c950 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -5,7 +5,8 @@ import { ChannelSettingValue, ChannelSettings, ClientEvents, - Participant + Participant, + ServerEvents } from "../util/types"; import { Socket } from "../ws/Socket"; import { validateChannelSettings } from "./settings"; @@ -48,6 +49,7 @@ export class Channel extends EventEmitter { private ppl = new Array(); public logger: Logger; + public chatHistory = new Array(); // TODO Add the crown @@ -165,6 +167,10 @@ export class Channel extends EventEmitter { ch: this.getInfo(), p: part.id, ppl: this.getParticipantList() + }, + { + m: "c", + c: this.chatHistory.slice(-50) } ]); @@ -254,17 +260,16 @@ export class Channel extends EventEmitter { public sendArray( arr: ClientEvents[EventID][] ) { - let i = 0; - for (const socket of socketsBySocketID.values()) { - for (const p of this.ppl) { - if (p.id == socket.getParticipantID()) { - // this.logger.debug( - // `Sending to ${socket.getParticipant()?.name} [${i}]:`, - // arr - // ); - socket.sendArray(arr); - i++; - } + let sentSocketIDs = new Array(); + + for (const p of this.ppl) { + socketLoop: for (const socket of socketsBySocketID.values()) { + if (socket.isDestroyed()) continue socketLoop; + if (socket.getParticipantID() != p.id) continue socketLoop; + if (sentSocketIDs.includes(socket.socketID)) + continue socketLoop; + socket.sendArray(arr); + sentSocketIDs.push(socket.socketID); } } } @@ -287,6 +292,20 @@ export class Channel extends EventEmitter { } } }); + + this.on("a", (msg: ServerEvents["a"], socket: Socket) => { + if (!msg.message) return; + + let outgoing: ClientEvents["a"] = { + m: "a", + a: msg.message, + t: Date.now(), + p: socket.getParticipant() as Participant + }; + + this.sendArray([outgoing]); + this.chatHistory.push(outgoing); + }); } } diff --git a/src/ws/events/user/handlers/a.ts b/src/ws/events/user/handlers/a.ts new file mode 100644 index 0000000..6668a71 --- /dev/null +++ b/src/ws/events/user/handlers/a.ts @@ -0,0 +1,12 @@ +import { ServerEventListener } from "../../../../util/types"; + +export const a: ServerEventListener<"a"> = { + id: "a", + callback: (msg, socket) => { + // Send chat message + const ch = socket.getCurrentChannel(); + if (!ch) return; + + ch.emit("a", msg, socket); + } +}; diff --git a/src/ws/events/user/handlers/ch.ts b/src/ws/events/user/handlers/ch.ts index 303bd1b..fe92514 100644 --- a/src/ws/events/user/handlers/ch.ts +++ b/src/ws/events/user/handlers/ch.ts @@ -1,5 +1,4 @@ import { ServerEventListener } from "../../../../util/types"; -import { eventGroups } from "../../../events"; export const ch: ServerEventListener<"ch"> = { id: "ch", diff --git a/src/ws/events/user/handlers/devices.ts b/src/ws/events/user/handlers/devices.ts index 6555650..72b80b5 100644 --- a/src/ws/events/user/handlers/devices.ts +++ b/src/ws/events/user/handlers/devices.ts @@ -1,5 +1,4 @@ import { ServerEventListener } from "../../../../util/types"; -import { eventGroups } from "../../../events"; export const devices: ServerEventListener<"devices"> = { id: "devices", diff --git a/src/ws/events/user/handlers/hi.ts b/src/ws/events/user/handlers/hi.ts index e291670..d5706b5 100644 --- a/src/ws/events/user/handlers/hi.ts +++ b/src/ws/events/user/handlers/hi.ts @@ -1,5 +1,4 @@ import { ServerEventListener } from "../../../../util/types"; -import { eventGroups } from "../../../events"; export const hi: ServerEventListener<"hi"> = { id: "hi", diff --git a/src/ws/events/user/handlers/m.ts b/src/ws/events/user/handlers/m.ts index ba80cf5..38a9ba3 100644 --- a/src/ws/events/user/handlers/m.ts +++ b/src/ws/events/user/handlers/m.ts @@ -1,5 +1,4 @@ import { ServerEventListener } from "../../../../util/types"; -import { eventGroups } from "../../../events"; export const m: ServerEventListener<"m"> = { id: "m", diff --git a/src/ws/events/user/index.ts b/src/ws/events/user/index.ts index 94ff256..0dfd214 100644 --- a/src/ws/events/user/index.ts +++ b/src/ws/events/user/index.ts @@ -6,10 +6,12 @@ import { hi } from "./handlers/hi"; import { devices } from "./handlers/devices"; import { ch } from "./handlers/ch"; import { m } from "./handlers/m"; +import { a } from "./handlers/a"; EVENTGROUP_USER.add(hi); EVENTGROUP_USER.add(devices); EVENTGROUP_USER.add(ch); EVENTGROUP_USER.add(m); +EVENTGROUP_USER.add(a); eventGroups.push(EVENTGROUP_USER); diff --git a/src/ws/message.ts b/src/ws/message.ts index 841a96b..2447952 100644 --- a/src/ws/message.ts +++ b/src/ws/message.ts @@ -18,6 +18,7 @@ export function handleMessage(socket: Socket, text: string) { } else { for (const msg of transmission) { if (!hasOwn(msg, "m")) continue; + if (socket.isDestroyed()) continue; socket.emit(msg.m, msg, socket); } }