RSS Git Download  Clone
Raw Blame History 1kB 44 lines
const sharedIoRedis = require('../shared')

module.exports = async(options) => {
    const { socket } = options;

    const connectionSaveId = options.payload.id;
    let connectionIndexExisting;
    for(let connectionIndex in p3xrs.connections.list) {
        const connection = p3xrs.connections.list[connectionIndex]
        if (connection.id === connectionSaveId) {
            connectionIndexExisting = connectionIndex
            break;
        }
    }
    try {
        if (connectionIndexExisting !== undefined) {
            p3xrs.connections.list.splice(connectionIndexExisting, 1)
            p3xrs.connections.update = new Date()
            const fs = require('fs')
            fs.writeFileSync(p3xrs.cfg.connections.home, JSON.stringify(p3xrs.connections, null, 4))
        }
        socket.emit(options.responseEvent, {
            status: 'ok',
        })
    } catch (error) {
        console.log(error)
        socket.emit(options.responseEvent, {
            status: 'error',
            error: error
        })
    } finally {
        sharedIoRedis.sendConnections({
            socket: socket,
        })


        sharedIoRedis.triggerDisconnect({
            connectionId: connectionSaveId,
            code: 'delete-connection',
            socket: socket,
        })
    }

}