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

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

    const connectionSave = options.payload.model;
    let connectionIndexExisting;
    for(let connectionIndex in p3xrs.connections.list) {
        const connection = p3xrs.connections.list[connectionIndex]
        if (connection.id === connectionSave.id) {
            connectionIndexExisting = connectionIndex
            break;
        }
    }
    p3xrs.connections.update = new Date()
    if (connectionIndexExisting !== undefined) {
        p3xrs.connections.list[connectionIndexExisting] = connectionSave
    } else {
        p3xrs.connections.list.push(connectionSave)
    }
    try {
        const fs = require('fs')
        fs.writeFileSync(p3xrs.cfg.connections.home, JSON.stringify(p3xrs.connections, null, 4))

        socket.emit(options.responseEvent, {
            status: 'ok',
        })

    } catch (e) {
        console.error(e)
        socket.emit(options.responseEvent, {
            status: 'error',
            error: error
        })

    } finally {
        sharedIoRedis.sendConnections({
            socket: socket,
        })

        sharedIoRedis.triggerDisconnect({
            connectionId: connectionSave.id,
            code: 'save-connection',
            socket: socket,

        })
    }

}