refactor: add logs and fix connection listener
parent
4b5140ce8b
commit
e51fbca365
|
@ -3,25 +3,23 @@ let io
|
|||
|
||||
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 extension = socket.handshake.auth.extension
|
||||
const host = socket.handshake.headers.host
|
||||
|
||||
if (!host) {
|
||||
return next(new Error(`Host must be created`))
|
||||
const origin = socket.handshake.headers.origin
|
||||
if (!origin) {
|
||||
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(`Invalid handshake header information information origin must be specified`))
|
||||
}
|
||||
|
||||
const isFromHitphoneWebClient = true
|
||||
// const isFromHitphoneWebClient = host.includes("https://ms-teamsapp.omnihit.app.br/")
|
||||
const isFromHitphoneWebClient = origin.includes(process.env.URL_HITPHONE_FRONTEND)
|
||||
if (!isFromHitphoneWebClient) {
|
||||
socket.data.isFromHitphoneWebClient = false
|
||||
next()
|
||||
return next()
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -33,8 +31,8 @@ const onSocketHandshakeAuthVerifier = (socket, next) => {
|
|||
const onConnectionHitphoneWebClient = (socket) => {
|
||||
const { isFromHitphoneWebClient } = socket.data
|
||||
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
|
||||
socket.join(`${codWeb}@${extension}`)
|
||||
|
||||
|
@ -45,14 +43,14 @@ const onConnectionHitphoneWebClient = (socket) => {
|
|||
|
||||
const onConnectionCrmWizardClient = (socket) => {
|
||||
const { isFromHitphoneWebClient } = socket.data
|
||||
if (!isFromHitphoneWebClient) return
|
||||
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
|
||||
if (isFromHitphoneWebClient) return
|
||||
|
||||
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "CRM Wizard client"`)
|
||||
const { codWeb, extension } = socket.data
|
||||
socket.join(`${codWeb}@${extension}`)
|
||||
|
||||
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
|
||||
*/
|
||||
io.use("connection", onConnectionCrmWizardClient)
|
||||
io.on("connection", onConnectionCrmWizardClient)
|
||||
/**
|
||||
* Hitphone Client Flow
|
||||
*/
|
||||
io.use("connection", onConnectionHitphoneWebClient)
|
||||
io.on("connection", onConnectionHitphoneWebClient)
|
||||
|
||||
return io
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue