discard auth methods

This commit is contained in:
srgooglo 2022-02-21 09:55:30 +01:00
parent e7abe3157e
commit ebdac8631c

View File

@ -60,24 +60,7 @@ class Server {
this.wsInterface = global.wsInterface = { this.wsInterface = global.wsInterface = {
io: new io.Server(this.WSListenPort), io: new io.Server(this.WSListenPort),
map: {}, map: {},
clients: [],
eventsChannels: [], eventsChannels: [],
getChannelsEventsFromNSP: (nsp) => {
return this.wsInterface.eventsChannels.filter(channel => channel.nsp === nsp)
},
findUserIdFromClientID: (clientId) => {
return this.wsInterface.clients.find(client => client.id === clientId)?.userId ?? false
},
getClientSockets: (userId) => {
return this.wsInterface.clients.filter(client => client.userId === userId).map((client) => {
return client?.socket
})
},
broadcast: async (channel, ...args) => {
for await (const client of this.wsInterface.clients) {
client.socket.emit(channel, ...args)
}
},
} }
//? check if origin.server exists //? check if origin.server exists
@ -145,6 +128,10 @@ class Server {
socket.emit("responseError", ...args) socket.emit("responseError", ...args)
} }
if (typeof this.params.onWSClientConnection === "function") {
await this.params.onWSClientConnection(socket)
}
for await (const [nsp, on, dispatch] of this.wsInterface.eventsChannels) { for await (const [nsp, on, dispatch] of this.wsInterface.eventsChannels) {
socket.on(on, async (...args) => { socket.on(on, async (...args) => {
try { try {
@ -158,55 +145,17 @@ class Server {
}) })
} }
let authResult = true
if (typeof this.params.wsAuthorization === "function") {
authResult = await this.params.wsAuthorization(socket)
}
if (authResult) {
this.attachClientSocket(socket, socket.id)
} else {
socket.disconnect()
}
socket.on("ping", () => { socket.on("ping", () => {
socket.emit("pong") socket.emit("pong")
}) })
socket.on("disconnect", () => { socket.on("disconnect", async () => {
console.log(`[${socket.id}] disconnected`) if (typeof this.params.onWSClientDisconnect === "function") {
this.detachClientSocket(socket) await this.params.onWSClientDisconnect(socket)
}
}) })
} }
attachClientSocket = async (client, userId) => {
const socket = this.wsInterface.clients.find(c => c.id === client.id)
if (socket) {
socket.socket.disconnect()
}
this.wsInterface.clients.push({
id: client.id,
socket: client,
userId,
})
this.wsInterface.io.emit("user_connected", userId)
}
detachClientSocket = async (client) => {
const socket = this.wsInterface.clients.find(c => c.id === client.id)
if (socket) {
socket.socket.disconnect()
this.wsInterface.clients = this.wsInterface.clients.filter(c => c.id !== client.id)
}
this.wsInterface.io.emit("user_disconnected", client.id)
}
initializeControllers = async () => { initializeControllers = async () => {
for await (let controller of this.controllers) { for await (let controller of this.controllers) {
if (typeof controller !== "function") { if (typeof controller !== "function") {