eslint done __up to date pushing for review
This commit is contained in:
parent
d43e62dfc1
commit
b07040b61b
2 changed files with 39 additions and 34 deletions
|
|
@ -6,7 +6,7 @@ import * as auth from '@shared/auth';
|
||||||
import * as swagger from '@shared/swagger';
|
import * as swagger from '@shared/swagger';
|
||||||
import * as utils from '@shared/utils';
|
import * as utils from '@shared/utils';
|
||||||
import useSocketIo from 'fastify-socket.io';
|
import useSocketIo from 'fastify-socket.io';
|
||||||
import { setupSocketIo } from "./socket";
|
import { setupSocketIo } from './socket';
|
||||||
|
|
||||||
declare const __SERVICE_NAME: string;
|
declare const __SERVICE_NAME: string;
|
||||||
|
|
||||||
|
|
@ -40,8 +40,6 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||||
|
|
||||||
// Setup Socket.io
|
// Setup Socket.io
|
||||||
setupSocketIo(fastify);
|
setupSocketIo(fastify);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
export { app };
|
export { app };
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,17 @@
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
|
||||||
export const color = {
|
export const color = {
|
||||||
red: "\x1b[31m",
|
red: 'x1b[31m',
|
||||||
green: "\x1b[32m",
|
green: 'x1b[32m',
|
||||||
yellow: "\x1b[33m",
|
yellow: 'x1b[33m',
|
||||||
blue: "\x1b[34m",
|
blue: 'x1b[34m',
|
||||||
reset: "\x1b[0m",
|
reset: 'x1b[0m',
|
||||||
|
};
|
||||||
|
|
||||||
|
type ClientMessage = {
|
||||||
|
userID: string;
|
||||||
|
text: string;
|
||||||
|
SenderWindowID: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// When using .decorate you have to specify added properties for Typescript
|
// When using .decorate you have to specify added properties for Typescript
|
||||||
|
|
@ -12,7 +19,7 @@ declare module 'fastify' {
|
||||||
interface FastifyInstance {
|
interface FastifyInstance {
|
||||||
io: Server<{
|
io: Server<{
|
||||||
hello: (message: string) => string,
|
hello: (message: string) => string,
|
||||||
MsgObjectServer: (data: { message: string }) => void,
|
MsgObjectServer: (data: { message: ClientMessage }) => void,
|
||||||
message: (msg: string) => void,
|
message: (msg: string) => void,
|
||||||
testend: (sock_id_client: string) => void,
|
testend: (sock_id_client: string) => void,
|
||||||
}>
|
}>
|
||||||
|
|
@ -20,45 +27,45 @@ declare module 'fastify' {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function setupSocketIo(fastify: import('fastify').FastifyInstance): void {
|
export function setupSocketIo(fastify: import('fastify').FastifyInstance): void {
|
||||||
|
|
||||||
fastify.ready((err) => {
|
fastify.ready((err) => {
|
||||||
if (err) throw 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) {
|
// Broadcast function to send messages to all connected clients except the sender
|
||||||
if (s.id !== sender) {
|
function broadcast(data: ClientMessage, sender?: string) {
|
||||||
// Send REAL JSON object
|
fastify.io.fetchSockets().then((sockets) => {
|
||||||
s.emit("MsgObjectServer", { message: data });
|
console.log('Connected clients:', sockets.length);
|
||||||
|
|
||||||
console.log(" emit window socket ID:", s.id);
|
for (const s of sockets) {
|
||||||
console.log(" emit window ID:", [...s.rooms]);
|
if (s.id !== sender) {
|
||||||
console.log(" Sender window ID:", sender ? sender : "none");
|
// Send REAL JSON object
|
||||||
console.log(" text recieved:", data.text ? data.text : "none");
|
s.emit('MsgObjectServer', { message: data });
|
||||||
console.log(color.red, "data:", color.reset, data ? data : "none");
|
|
||||||
|
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)));
|
// console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(fastify.io)));
|
||||||
fastify.io.on('connection', (socket : Socket) => {
|
fastify.io.on('connection', (socket : Socket) => {
|
||||||
console.info(color.blue, 'Socket connected!', color.reset, socket.id);
|
console.info(color.blue, 'Socket connected!', color.reset, socket.id);
|
||||||
socket.on("message", (message: string) => {
|
socket.on('message', (message: string) => {
|
||||||
console.log(color.blue, `Received message from client`, color.reset, message);
|
console.log(color.blue, 'Received message from client', color.reset, message);
|
||||||
const obj = JSON.parse(message); // { userID, text }
|
const obj: ClientMessage = JSON.parse(message) as ClientMessage;
|
||||||
console.log(color.green, `Message from client`, color.reset, `${obj.userID}: ${obj.text}`);
|
console.log(color.green, 'Message from client', color.reset, `${obj.userID}: ${obj.text}`);
|
||||||
|
|
||||||
// Send object directly — DO NOT wrap it in a string
|
// Send object directly — DO NOT wrap it in a string
|
||||||
broadcast(obj, obj.SenderWindowID);
|
broadcast(obj, obj.SenderWindowID);
|
||||||
});
|
});
|
||||||
socket.on('testend', (sock_id_cl : string) => {
|
socket.on('testend', (sock_id_cl : string) => {
|
||||||
console.log('testend received from client socket id:', sock_id_cl);
|
console.log('testend received from client socket id:', sock_id_cl);
|
||||||
});
|
});
|
||||||
socket.on('disconnecting', (reason ) => {
|
socket.on('disconnecting', (reason) => {
|
||||||
console.log("Client is disconnecting:", socket.id, "reason:", reason);
|
console.log('Client is disconnecting:', socket.id, 'reason:', reason);
|
||||||
console.log('Socket AAAAAAAActing because:', socket.connected);
|
console.log('Socket AAAAAAAActing because:', socket.connected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue