2022-01-06 01:26:15 +00:00
|
|
|
import { Server as SocketIO } from "socket.io";
|
|
|
|
import { Server } from "http";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
let io: SocketIO;
|
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
//test del
|
2022-05-06 22:49:45 +00:00
|
|
|
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
|
|
|
|
2022-05-06 22:49:45 +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
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const initIO = (httpServer: Server): SocketIO => {
|
|
|
|
io = new SocketIO(httpServer, {
|
|
|
|
cors: {
|
|
|
|
origin: process.env.FRONTEND_URL
|
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
});
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
io.on("connection", socket => {
|
2022-05-06 22:49:45 +00:00
|
|
|
logger.info("Client Connected");
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
|
2022-05-06 22:49:45 +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
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
const onlineUser = await createOrUpdateOnlineUserService({ userId: userId, status: 'online' })
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' })
|
2022-04-27 17:28:31 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
countOnline = onlineUsers.length
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
console.log(' PUSHED NEW ID: ', userId)
|
|
|
|
}
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
|
2022-05-06 22:49:45 +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
|
|
|
|
2022-05-06 22:49:45 +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
|
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
obj.uuid = uuidv4()
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
exports.ob = obj
|
|
|
|
|
|
|
|
count++
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2022-04-27 17:28:31 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
// exports.listOnlineUsers = listUserId
|
|
|
|
// exports.listUserId
|
|
|
|
|