eslint done __up to date pushing for review

This commit is contained in:
NigeParis 2025-11-20 13:11:13 +01:00 committed by Maix0
parent d43e62dfc1
commit b07040b61b
2 changed files with 39 additions and 34 deletions

View file

@ -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<void> => {
// Setup Socket.io
setupSocketIo(fastify);
};
export default app;
export { app };

View file

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