chat general broadcast done with now notifications system separated in different space on the chat windo
This commit is contained in:
parent
26c9bd6421
commit
8a151cfb5e
6 changed files with 81 additions and 78 deletions
|
|
@ -32,6 +32,13 @@ interface ClientInfo {
|
|||
lastSeen: number;
|
||||
}
|
||||
|
||||
export type ClientMessage = {
|
||||
destination: string;
|
||||
user: string;
|
||||
text: string;
|
||||
SenderWindowID: string;
|
||||
};
|
||||
|
||||
const clientChat = new Map<string, ClientInfo>();
|
||||
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
|
|
@ -69,18 +76,13 @@ export default app;
|
|||
export { app };
|
||||
|
||||
|
||||
type ClientMessage = {
|
||||
user: string;
|
||||
text: string;
|
||||
SenderWindowID: string;
|
||||
};
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
io: Server<{
|
||||
hello: (message: string) => string;
|
||||
MsgObjectServer: (data: { message: ClientMessage }) => void;
|
||||
MsgObjectServer: (data: { message: ClientMessage } ) => void;
|
||||
message: (msg: string) => void;
|
||||
listBud: (msg: string) => void;
|
||||
testend: (sock_id_client: string) => void;
|
||||
|
|
@ -273,6 +275,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
if (!clientName) return;
|
||||
console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
type: "chat" as const,
|
||||
user: clientName,
|
||||
token: "",
|
||||
|
|
@ -300,6 +303,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
|
|
@ -323,6 +327,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
|
|
@ -367,6 +372,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
);
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
frontendUserName: userNameFromFrontend,
|
||||
|
|
|
|||
|
|
@ -34,54 +34,6 @@ export const ChatRes = {
|
|||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
|
||||
function connectedUser(io: Server | undefined, targetSocketId?: string): number {
|
||||
let count = 0;
|
||||
|
||||
// Track unique usernames (avoid duplicates)
|
||||
const seenUsers = new Set<string>();
|
||||
|
||||
for (const [socketId, info] of clientChat) {
|
||||
|
||||
// Validate entry
|
||||
if (!info || typeof info.user !== "string" || info.user.trim() === "") {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
const username = info.user;
|
||||
|
||||
// Validate socket exists if io is passed
|
||||
if (io) {
|
||||
const socket = io.sockets.sockets.get(socketId);
|
||||
|
||||
// Remove disconnected sockets
|
||||
if (!socket || socket.disconnected) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip duplicates
|
||||
if (seenUsers.has(username))
|
||||
continue;
|
||||
|
||||
seenUsers.add(username);
|
||||
count++;
|
||||
|
||||
// Send to target only
|
||||
if (io && targetSocketId) {
|
||||
io.to(targetSocketId).emit("listBud", username);
|
||||
}
|
||||
|
||||
console.log(color.yellow, "Client:", color.reset, username);
|
||||
console.log(color.yellow, "Socket ID:", color.reset, socketId);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
fastify.get(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue