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,
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<Participant>();
public logger: Logger;
public chatHistory = new Array<ClientEvents["a"]>();
// 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<EventID extends keyof ClientEvents>(
arr: ClientEvents[EventID][]
) {
let i = 0;
for (const socket of socketsBySocketID.values()) {
let sentSocketIDs = new Array<string>();
for (const p of this.ppl) {
if (p.id == socket.getParticipantID()) {
// this.logger.debug(
// `Sending to ${socket.getParticipant()?.name} [${i}]:`,
// arr
// );
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);
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 { eventGroups } from "../../../events";
export const ch: ServerEventListener<"ch"> = {
id: "ch",

View File

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

View File

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

View File

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

View File

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

View File

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