broadcast generally working

This commit is contained in:
NigeParis 2025-11-20 12:05:49 +01:00 committed by Maix0
parent 98631be918
commit 486f0ff425
2 changed files with 79 additions and 62 deletions

View file

@ -28,53 +28,61 @@ export function setupSocketIo(fastify: import('fastify').FastifyInstance): void
fastify.ready((err) => {
if (err) throw err;
function broadcast(message: any, sender?: any) {
// function broadcast(message: any, sender?: any) {
fastify.io.fetchSockets().then((sockets) => {
console.log("Connected clients:", sockets.length);
for (const s of sockets) {
// fastify.io.fetchSockets().then((sockets) => {
// console.log("Connected clients:", sockets.length);
// for (const s of sockets) {
if (s.id !== sender) {
s.emit('MsgObjectServer',{ message: `${message}` });
console.log("Socket ID:", s.id);
console.log("Rooms:", [...s.rooms]);
console.log("Sender:", sender ? sender : 'none');
}
}
});
}
// if (s.id !== sender) {
// s.emit('MsgObjectServer',{ message: `${message}` });
// console.log("Socket ID:", s.id);
// console.log("Rooms:", [...s.rooms]);
// console.log("Sender:", sender ? sender : 'none');
// console.log("ID:", message.text ? message.text : 'none');
// }
// }
// });
// }
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 });
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('Socket connected!', socket.id);
// socket.on('hello', (value) => {
// return 'hi';
// });
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}`);
// fastify.io.fetchSockets().then((sockets) => {
// console.log("Connected clients:", sockets.length);
// for (const s of sockets) {
// console.log("Socket ID:", s.id);
// console.log("Rooms:", [...s.rooms]);
// }
// });
// socket.emit("MsgObjectServer", {message: `THIS IS A SERVER MESSAGE`});
socket.on('message', (message: string) => {
// console.log(color.red + `GOT MESSAGE ${color.reset} ${value}`);
console.log(color.green + `GOT MESSAGE from client ${socket.id}: ${color.reset} ${message}`);
const obj = JSON.parse(message);
const userID = obj.userID;
console.log(`Message from client ${obj.userID}: ${obj.text}`);
broadcast(`Broadcast from THIS server: ${obj.text}`,userID);
// Send object directly — DO NOT wrap it in a string
broadcast(obj, obj.SenderWindowID);
});
// socket.on('MsgObjectServer', (value) => { console.log(`GOT COUCOU ${value.message}`)
//broadcast(`Broadcast from server:`, socket.id );
// });
socket.on('testend', (sock_id_cl : string) => {
console.log('testend received from client socket id:', sock_id_cl);
});
@ -86,11 +94,7 @@ export function setupSocketIo(fastify: import('fastify').FastifyInstance): void
});
// fastify.io.on('disconnect', (socket : Socket) => {
// console.log('weeeeeeeeeeeewoooooooooooooooooooooooooo');
// console.log('alert in the high castle');
// console.log('Socket disconnected!', socket.id);
// });
});
};