⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.60
Server IP:
68.65.123.197
Server:
Linux premium49.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
Server Software:
LiteSpeed
PHP Version:
8.2.29
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
smarbgfw
/
whatsapp.smartech-hub.com
/
View File Name :
socket.js
const socketIO = require("socket.io"); const { query } = require("./database/dbpromise"); let ioInstance; // Global variable to store the io instance function initializeSocket(server) { const io = socketIO(server, { cors: { origin: process.env.FRONTENDURI, methods: ["GET", "POST"], }, }); // Save the io instance to the global variable ioInstance = io; // Socket.IO event handling io.on("connection", (socket) => { console.log("A user connected", socket.id); socket.on("user_connected", async ({ userId }) => { console.log({ userId }); if (userId) { console.log( `User ${userId?.slice(0, 5)} connected with socket ID: ${socket.id}` ); try { // Perform database operations within a try-catch block for error handling await query(`DELETE FROM rooms WHERE uid = ?`, [userId]); await query(`INSERT INTO rooms (uid, socket_id) VALUES (?, ?)`, [ userId, socket.id, ]); } catch (error) { console.error("Error executing database queries:", error); // Handle error gracefully, such as sending an error response to the client } } }); socket.on("disconnect", async () => { console.log(`A user disconnected with socket ID: ${socket.id}`); try { await query(`DELETE FROM rooms WHERE socket_id = ?`, [socket.id]); } catch (error) { console.error("Error executing database query:", error); } }); }); return io; // Return io instance } // Export a function to get the io instance function getIOInstance() { return ioInstance; } module.exports = { initializeSocket, getIOInstance };