Implement chat and fix duplicate message bug

This commit is contained in:
Hri7566 2023-09-10 06:36:41 -04:00
parent 16219b504a
commit d99523d94f
8 changed files with 46 additions and 16 deletions

View File

@ -5,7 +5,8 @@ import {
ChannelSettingValue, ChannelSettingValue,
ChannelSettings, ChannelSettings,
ClientEvents, ClientEvents,
Participant Participant,
ServerEvents
} from "../util/types"; } from "../util/types";
import { Socket } from "../ws/Socket"; import { Socket } from "../ws/Socket";
import { validateChannelSettings } from "./settings"; import { validateChannelSettings } from "./settings";
@ -48,6 +49,7 @@ export class Channel extends EventEmitter {
private ppl = new Array<Participant>(); private ppl = new Array<Participant>();
public logger: Logger; public logger: Logger;
public chatHistory = new Array<ClientEvents["a"]>();
// TODO Add the crown // TODO Add the crown
@ -165,6 +167,10 @@ export class Channel extends EventEmitter {
ch: this.getInfo(), ch: this.getInfo(),
p: part.id, p: part.id,
ppl: this.getParticipantList() ppl: this.getParticipantList()
},
{
m: "c",
c: this.chatHistory.slice(-50)
} }
]); ]);
@ -254,17 +260,16 @@ export class Channel extends EventEmitter {
public sendArray<EventID extends keyof ClientEvents>( public sendArray<EventID extends keyof ClientEvents>(
arr: ClientEvents[EventID][] arr: ClientEvents[EventID][]
) { ) {
let i = 0; let sentSocketIDs = new Array<string>();
for (const socket of socketsBySocketID.values()) {
for (const p of this.ppl) { for (const p of this.ppl) {
if (p.id == socket.getParticipantID()) { socketLoop: for (const socket of socketsBySocketID.values()) {
// this.logger.debug( if (socket.isDestroyed()) continue socketLoop;
// `Sending to ${socket.getParticipant()?.name} [${i}]:`, if (socket.getParticipantID() != p.id) continue socketLoop;
// arr if (sentSocketIDs.includes(socket.socketID))
// ); continue socketLoop;
socket.sendArray(arr); socket.sendArray(arr);
i++; 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);
});
} }
} }

View File

@ -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);
}
};

View File

@ -1,5 +1,4 @@
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
import { eventGroups } from "../../../events";
export const ch: ServerEventListener<"ch"> = { export const ch: ServerEventListener<"ch"> = {
id: "ch", id: "ch",

View File

@ -1,5 +1,4 @@
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
import { eventGroups } from "../../../events";
export const devices: ServerEventListener<"devices"> = { export const devices: ServerEventListener<"devices"> = {
id: "devices", id: "devices",

View File

@ -1,5 +1,4 @@
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
import { eventGroups } from "../../../events";
export const hi: ServerEventListener<"hi"> = { export const hi: ServerEventListener<"hi"> = {
id: "hi", id: "hi",

View File

@ -1,5 +1,4 @@
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
import { eventGroups } from "../../../events";
export const m: ServerEventListener<"m"> = { export const m: ServerEventListener<"m"> = {
id: "m", id: "m",

View File

@ -6,10 +6,12 @@ import { hi } from "./handlers/hi";
import { devices } from "./handlers/devices"; import { devices } from "./handlers/devices";
import { ch } from "./handlers/ch"; import { ch } from "./handlers/ch";
import { m } from "./handlers/m"; import { m } from "./handlers/m";
import { a } from "./handlers/a";
EVENTGROUP_USER.add(hi); EVENTGROUP_USER.add(hi);
EVENTGROUP_USER.add(devices); EVENTGROUP_USER.add(devices);
EVENTGROUP_USER.add(ch); EVENTGROUP_USER.add(ch);
EVENTGROUP_USER.add(m); EVENTGROUP_USER.add(m);
EVENTGROUP_USER.add(a);
eventGroups.push(EVENTGROUP_USER); eventGroups.push(EVENTGROUP_USER);

View File

@ -18,6 +18,7 @@ export function handleMessage(socket: Socket, text: string) {
} else { } else {
for (const msg of transmission) { for (const msg of transmission) {
if (!hasOwn(msg, "m")) continue; if (!hasOwn(msg, "m")) continue;
if (socket.isDestroyed()) continue;
socket.emit(msg.m, msg, socket); socket.emit(msg.m, msg, socket);
} }
} }