From b07040b61bad36afcb1a6a9f0836c497d44c6c65 Mon Sep 17 00:00:00 2001 From: NigeParis Date: Thu, 20 Nov 2025 13:11:13 +0100 Subject: [PATCH] eslint done __up to date pushing for review --- src/chat/src/app.ts | 4 +-- src/chat/src/socket.ts | 69 +++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index 1995e41..fda03a5 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -6,7 +6,7 @@ import * as auth from '@shared/auth'; import * as swagger from '@shared/swagger'; import * as utils from '@shared/utils'; import useSocketIo from 'fastify-socket.io'; -import { setupSocketIo } from "./socket"; +import { setupSocketIo } from './socket'; declare const __SERVICE_NAME: string; @@ -40,8 +40,6 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise => { // Setup Socket.io setupSocketIo(fastify); - }; - export default app; export { app }; diff --git a/src/chat/src/socket.ts b/src/chat/src/socket.ts index 205e5f5..223e1cc 100644 --- a/src/chat/src/socket.ts +++ b/src/chat/src/socket.ts @@ -1,10 +1,17 @@ import { Server, Socket } from 'socket.io'; + export const color = { - red: "\x1b[31m", - green: "\x1b[32m", - yellow: "\x1b[33m", - blue: "\x1b[34m", - reset: "\x1b[0m", + red: 'x1b[31m', + green: 'x1b[32m', + yellow: 'x1b[33m', + blue: 'x1b[34m', + reset: 'x1b[0m', +}; + +type ClientMessage = { + userID: string; + text: string; + SenderWindowID: string; }; // When using .decorate you have to specify added properties for Typescript @@ -12,7 +19,7 @@ declare module 'fastify' { interface FastifyInstance { io: Server<{ hello: (message: string) => string, - MsgObjectServer: (data: { message: string }) => void, + MsgObjectServer: (data: { message: ClientMessage }) => void, message: (msg: string) => void, testend: (sock_id_client: string) => void, }> @@ -20,45 +27,45 @@ declare module 'fastify' { }; export function setupSocketIo(fastify: import('fastify').FastifyInstance): void { - + fastify.ready((err) => { if (err) throw err; - - // Broadcast function to send messages to all connected clients except the sender - function broadcast(data: any, sender?: string) { - fastify.io.fetchSockets().then((sockets) => { - console.log("Connected clients:", sockets.length); - for (const s of sockets) { - if (s.id !== sender) { - // Send REAL JSON object - s.emit("MsgObjectServer", { message: data }); + // Broadcast function to send messages to all connected clients except the sender + function broadcast(data: ClientMessage, sender?: string) { + fastify.io.fetchSockets().then((sockets) => { + console.log('Connected clients:', sockets.length); - console.log(" emit window socket ID:", s.id); - console.log(" emit window ID:", [...s.rooms]); - console.log(" Sender window ID:", sender ? sender : "none"); - console.log(" text recieved:", data.text ? data.text : "none"); - console.log(color.red, "data:", color.reset, data ? data : "none"); + for (const s of sockets) { + if (s.id !== sender) { + // Send REAL JSON object + s.emit('MsgObjectServer', { message: data }); + + console.log(' emit window socket ID:', s.id); + console.log(' emit window ID:', [...s.rooms]); + console.log(' Sender window ID:', sender ? sender : 'none'); + console.log(' text recieved:', data.text ? data.text : 'none'); + console.log(color.red, 'data:', color.reset, data ? data : 'none'); + } } - } - }); - }; + }); + }; // console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(fastify.io))); fastify.io.on('connection', (socket : Socket) => { console.info(color.blue, 'Socket connected!', color.reset, socket.id); - socket.on("message", (message: string) => { - console.log(color.blue, `Received message from client`, color.reset, message); - const obj = JSON.parse(message); // { userID, text } - console.log(color.green, `Message from client`, color.reset, `${obj.userID}: ${obj.text}`); - + socket.on('message', (message: string) => { + console.log(color.blue, 'Received message from client', color.reset, message); + const obj: ClientMessage = JSON.parse(message) as ClientMessage; + console.log(color.green, 'Message from client', color.reset, `${obj.userID}: ${obj.text}`); + // Send object directly — DO NOT wrap it in a string broadcast(obj, obj.SenderWindowID); }); socket.on('testend', (sock_id_cl : string) => { console.log('testend received from client socket id:', sock_id_cl); }); - socket.on('disconnecting', (reason ) => { - console.log("Client is disconnecting:", socket.id, "reason:", reason); + socket.on('disconnecting', (reason) => { + console.log('Client is disconnecting:', socket.id, 'reason:', reason); console.log('Socket AAAAAAAActing because:', socket.connected); }); });