import { Server as SocketIO } from "socket.io"; import { Server } from "http"; import AppError from "../errors/AppError"; import { logger } from "../utils/logger"; let io: SocketIO; //test del let listOnlineUsers:any[]=[] let count:number = 0 // export const initIO = (httpServer: Server): SocketIO => { io = new SocketIO(httpServer, { cors: { origin: process.env.FRONTEND_URL } }); io.on("connection", socket => { logger.info("Client Connected"); socket.on("online", (userId: string) => { logger.info(`CLIENT ID ${userId}` ); const indexUser = listOnlineUsers.findIndex((e) => e.userId == userId) if(indexUser == -1){ //listOnlineUsers.push({userId: userId, status: 'online'}) } count++ //console.log('count: ', count) if(count >= listOnlineUsers.length){ //console.log('listOnlineUsers1: ', listOnlineUsers) count = 0 } //console.log('listOnlineUsers1: ', listOnlineUsers) }); socket.on("joinChatBox", (ticketId: string) => { logger.info("A client joined a ticket channel"); socket.join(ticketId); }); socket.on("joinNotification", () => { logger.info("A client joined notification channel"); socket.join("notification"); }); socket.on("joinTickets", (status: string) => { logger.info(`A client joined to ${status} tickets channel.`); socket.join(status); }); socket.on("disconnect", () => { logger.info("Client disconnected"); }); }); return io; }; export const getIO = (): SocketIO => { if (!io) { throw new AppError("Socket IO not initialized"); } return io; }; exports.listOnlineUsers = listOnlineUsers