Implement chat and fix duplicate message bug
This commit is contained in:
parent
16219b504a
commit
d99523d94f
|
@ -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()) {
|
||||
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<string>();
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
|
@ -1,5 +1,4 @@
|
|||
import { ServerEventListener } from "../../../../util/types";
|
||||
import { eventGroups } from "../../../events";
|
||||
|
||||
export const ch: ServerEventListener<"ch"> = {
|
||||
id: "ch",
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { ServerEventListener } from "../../../../util/types";
|
||||
import { eventGroups } from "../../../events";
|
||||
|
||||
export const devices: ServerEventListener<"devices"> = {
|
||||
id: "devices",
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { ServerEventListener } from "../../../../util/types";
|
||||
import { eventGroups } from "../../../events";
|
||||
|
||||
export const hi: ServerEventListener<"hi"> = {
|
||||
id: "hi",
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { ServerEventListener } from "../../../../util/types";
|
||||
import { eventGroups } from "../../../events";
|
||||
|
||||
export const m: ServerEventListener<"m"> = {
|
||||
id: "m",
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue