import { Server as SocketIO } from "socket.io"; import { Server } from "http"; import AppError from "../errors/AppError"; import { logger } from "../utils/logger"; import { v4 as uuidv4 } from 'uuid'; import ListUserParamiterService from "../services/UserServices/ListUserParamiterService"; import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns"; let io: SocketIO; //test del import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService"; import { splitDateTime } from "../helpers/SplitDateTime"; import format from 'date-fns/format'; import ptBR from 'date-fns/locale/pt-BR'; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; let count: number = 0 let listOnline: any[] = [] let listOnlineAux: any[] = [] let countOnline: number = 0 let obj: any = { listOnline: [], uuid: null, listOnlineAux: [] } let lstOnline: any[] = [] let lstOnlineAux: any[] = [] let lstTry: any[] = [] let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) 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: any) => { obj.uuid = uuidv4() if (userId.logoutUserId) { let index = lstOnline.findIndex((x: any) => x.id == userId.logoutUserId) if (index != -1) { lstOnline.splice(index, 1) } index = lstOnlineAux.findIndex((x: any) => x.id == userId.logoutUserId) if (index != -1) { lstOnlineAux.splice(index, 1) } return } if (lstOnline.length == 0) { const index = listOnlineAux.findIndex((e: any) => e.id == userId) if (index == -1) { listOnlineAux.push({ 'id': userId }) } else { console.log(' -------------- PULO FORA') return } lstOnline.push({ 'id': userId, 'status': 'online', 'try': 0 }) lstOnlineAux.push({ 'id': userId }) console.log(' 1 PUSHED NEW USER ID 1: ', userId) obj.listOnline = lstOnline } else { const indexAux = lstOnlineAux.findIndex((e: any) => e.id == userId) if (indexAux == -1) { lstOnlineAux.push({ 'id': userId }) } const index = lstOnline.findIndex((e: any) => e.id == userId) if (index == -1) { lstOnline.push({ 'id': userId, 'status': 'online', 'try': 0 }) console.log(' 2 PUSHED NEW USER ID: ', userId) obj.listOnline = lstOnline } else { if (countOnline > (lstOnline.length - 1)) { lstOnline.forEach((x: any) => { if (lstOnlineAux.map((e: any) => e.id).includes(x.id)) { x.try = 0 x.status = 'online' } }) var difference = lstOnline.filter((x: any) => !lstOnlineAux.map((e: any) => e.id).includes(x.id)); if (difference.length > 0) { difference.forEach((e) => { e.try += 1 if (e.try > 1) { e.status = 'waiting...' } if (e.try > 3) { const index = lstOnline.findIndex((x: any) => x.id == e.id) if (index != -1) { lstOnline.splice(index, 1) } } }) } obj.listOnline = lstOnline obj.listOnlineAux = lstOnlineAux lstOnlineAux = [] listOnlineAux = [] countOnline = -1 } countOnline++ } } exports.ob = obj }); 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 = listUserId // exports.listUserId