refactor: add logs and fix connection listener

feat/hitphone-socket-integration
Henrriky 2024-07-19 12:08:03 -03:00
parent 4b5140ce8b
commit e51fbca365
1 changed files with 16 additions and 18 deletions

View File

@ -3,25 +3,23 @@ let io
const onSocketHandshakeAuthVerifier = (socket, next) => { const onSocketHandshakeAuthVerifier = (socket, next) => {
console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket trying to connect with data ${JSON.stringify(socket.handshake.auth)}`) console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket trying to connect with data ${JSON.stringify(socket.handshake.auth)} and origin ${socket.handshake.headers.origin}`)
const codWeb = socket.handshake.auth.codWeb const codWeb = socket.handshake.auth.codWeb
const extension = socket.handshake.auth.extension const extension = socket.handshake.auth.extension
const host = socket.handshake.headers.host const origin = socket.handshake.headers.origin
if (!origin) {
if (!host) { console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send the origin`)
return next(new Error(`Host must be created`)) return next(new Error(`Invalid handshake header information information origin must be specified`))
} }
const isFromHitphoneWebClient = origin.includes(process.env.URL_HITPHONE_FRONTEND)
const isFromHitphoneWebClient = true
// const isFromHitphoneWebClient = host.includes("https://ms-teamsapp.omnihit.app.br/")
if (!isFromHitphoneWebClient) { if (!isFromHitphoneWebClient) {
socket.data.isFromHitphoneWebClient = false socket.data.isFromHitphoneWebClient = false
next() return next()
} }
if (!codWeb || !extension) { if (!codWeb || !extension) {
return next(new Error(`Invalid authentication information, required attributes host, codWeb, extension`)) console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send extension or codWeb`)
return next(new Error(`Invalid handshake auth information, required attributes codWeb, extension`))
} }
socket.data.codWeb = codWeb socket.data.codWeb = codWeb
@ -33,8 +31,8 @@ const onSocketHandshakeAuthVerifier = (socket, next) => {
const onConnectionHitphoneWebClient = (socket) => { const onConnectionHitphoneWebClient = (socket) => {
const { isFromHitphoneWebClient } = socket.data const { isFromHitphoneWebClient } = socket.data
if (!isFromHitphoneWebClient) return if (!isFromHitphoneWebClient) return
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
const { codWeb, extension } = socket.data const { codWeb, extension } = socket.data
socket.join(`${codWeb}@${extension}`) socket.join(`${codWeb}@${extension}`)
@ -45,14 +43,14 @@ const onConnectionHitphoneWebClient = (socket) => {
const onConnectionCrmWizardClient = (socket) => { const onConnectionCrmWizardClient = (socket) => {
const { isFromHitphoneWebClient } = socket.data const { isFromHitphoneWebClient } = socket.data
if (!isFromHitphoneWebClient) return if (isFromHitphoneWebClient) return
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "CRM Wizard client"`)
const { codWeb, extension } = socket.data const { codWeb, extension } = socket.data
socket.join(`${codWeb}@${extension}`) socket.join(`${codWeb}@${extension}`)
socket.on("disconnect", (data) => { socket.on("disconnect", (data) => {
console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "Hitphone WEB Client" Client disconnected socket: ${data}`) console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "CRM Wizard client" Client disconnected, data: ${data}`)
}) })
} }
@ -70,11 +68,11 @@ const initIO = (httpServer) => {
/** /**
* CRM Wizard Client * CRM Wizard Client
*/ */
io.use("connection", onConnectionCrmWizardClient) io.on("connection", onConnectionCrmWizardClient)
/** /**
* Hitphone Client Flow * Hitphone Client Flow
*/ */
io.use("connection", onConnectionHitphoneWebClient) io.on("connection", onConnectionHitphoneWebClient)
return io return io
} }