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";
|
|
|
|
|
|
|
|
let io: SocketIO;
|
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
//test del
|
|
|
|
let listOnlineUsers:any[]=[]
|
|
|
|
let count:number = 0
|
|
|
|
//
|
|
|
|
|
|
|
|
|
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-03 21:20:58 +00:00
|
|
|
});
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
io.on("connection", socket => {
|
2022-05-03 21:20:58 +00:00
|
|
|
logger.info("Client Connected");
|
|
|
|
|
|
|
|
|
|
|
|
socket.on("online", (userId: string) => {
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(`CLIENT ID ${userId}` );
|
|
|
|
|
|
|
|
|
|
|
|
const indexUser = listOnlineUsers.findIndex((e) => e.userId == userId)
|
2022-04-27 17:28:31 +00:00
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
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
|
|
|
|
|
|
|
|
|
|
|
exports.listOnlineUsers = listOnlineUsers
|
|
|
|
|