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"; 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 listOnline2: any[] = [] let countOnline: number = 0 //let obj = { DESCENT_STEPS: 5000, ALPHA: 0.0005} let obj:any = { listOnline: [], listOnline2: [], uuid: null} 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", async (userId: string) => { console.log(' ----------------------------- listOnlie: ', obj.listOnline) const indexUser = obj.listOnline.findIndex((e:any) => e.id == userId) if (indexUser == -1) { obj.listOnline.push({ 'id': userId }) const onlineUser = await createOrUpdateOnlineUserService({ userId: userId, status: 'online' }) const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) countOnline = onlineUsers.length console.log(' PUSHED NEW ID: ', userId) } const index = obj.listOnline2.findIndex((e:any) => e.id == userId) if (index == -1) { obj.listOnline2.push({ 'id': userId }) } if (count > (countOnline - 1)) { var difference = obj.listOnline.filter((x:any) => !obj.listOnline2.map((e:any) => e.id).includes(x.id)); console.log('############> difference: ', difference) if (difference.length > 0) { difference.forEach(async (x:any) => { let index = obj.listOnline.findIndex((e:any) => e.id == x.id) if (index != -1) { obj.listOnline.splice(index, 1) const onlineUser = await createOrUpdateOnlineUserService({ userId: x.id, status: 'offline' }) const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) countOnline = onlineUsers.length } }) } obj.listOnline2 = [] count = 0 } obj.uuid = uuidv4() exports.ob = obj count++ }); 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