projeto-hit/backend/src/libs/socket.ts

144 lines
3.6 KiB
TypeScript
Raw Normal View History

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;
2022-05-03 21:20:58 +00:00
//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";
2022-05-03 21:20:58 +00:00
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 })))
//
2022-05-03 21:20:58 +00:00
export const initIO = (httpServer: Server): SocketIO => {
io = new SocketIO(httpServer, {
cors: {
origin: process.env.FRONTEND_URL
}
});
io.on("connection", socket => {
logger.info("Client Connected");
2022-05-03 21:20:58 +00:00
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 })
2022-05-03 21:20:58 +00:00
const onlineUser = await createOrUpdateOnlineUserService({ userId: userId, status: 'online' })
2022-05-03 21:20:58 +00:00
const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' })
countOnline = onlineUsers.length
2022-05-03 21:20:58 +00:00
console.log(' PUSHED NEW ID: ', userId)
}
2022-05-03 21:20:58 +00:00
const index = obj.listOnline2.findIndex((e:any) => e.id == userId)
if (index == -1) {
obj.listOnline2.push({ 'id': userId })
}
2022-05-03 21:20:58 +00:00
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 = []
2022-05-03 21:20:58 +00:00
count = 0
}
obj.uuid = uuidv4()
2022-05-03 21:20:58 +00:00
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;
};
2022-05-03 21:20:58 +00:00
// exports.listOnlineUsers = listUserId
// exports.listUserId