{"_id":"@allnulled/multisocket","name":"@allnulled/multisocket","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@allnulled/multisocket","version":"1.0.0","description":"Manage socket.io and socket.io-client communications and events from the same JavaScript class.","main":"multisocket.js","scripts":{"test":"node test.js"},"keywords":["socket.io","socket.io-client"],"author":{"name":"allnulled"},"license":"WTFPL","dependencies":{"socket.io":"^4.8.1","socket.io-client":"^4.8.1"},"gitHead":"121a4fc6da4e558832da7d996578787730dd9362","_id":"@allnulled/multisocket@1.0.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-V1KpwU9N/+45gj5wcvb1YLmFwb6IHQ/vPUavDbrOaVky50K5dfrAUuzsprwXASI/w0vrbBde0ExjWS4y3NJ38g==","shasum":"8554bcc91dea553dbd7807e60f99a806b5b04e6f","tarball":"https://registry.npmjs.org/@allnulled/multisocket/-/multisocket-1.0.0.tgz","fileCount":5,"unpackedSize":21669,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBoyqmdQunli1NZrmWP/VtKj1WvMCxIX46z4w1HUiZUUAiEAj3ilIbYVxN6GygU/i3dt49Lb4AGSu8Z0WPRoJymfnt4="}]},"_npmUser":{"name":"allnulled","email":"todosanulados@gmail.com"},"directories":{},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/multisocket_1.0.0_1734544132424_0.5644905880241207"},"_hasShrinkwrap":true}},"time":{"created":"2024-12-18T17:48:52.314Z","1.0.0":"2024-12-18T17:48:52.634Z","modified":"2024-12-18T17:48:52.931Z"},"maintainers":[{"name":"allnulled","email":"todosanulados@gmail.com"}],"description":"Manage socket.io and socket.io-client communications and events from the same JavaScript class.","keywords":["socket.io","socket.io-client"],"author":{"name":"allnulled"},"license":"WTFPL","readme":"# multisocket\n\nManage socket.io and socket.io-client communications and events from the same JavaScript class.\n\n## Installation\n\n```sh\nnpm i -s @allnulled/multisocket\n```\n\n## Usage\n\n```js\nconst Multisocket = require('@allnulled/multisocket');\n\nconst $msServer = new Multisocket(true, 3000);\nConfigure_server: {\n  $msServer.server.register('test', (data, $eventClient) => {\n    $eventClient.send({\n      type: 'response',\n      message: 'Respuesta del servidor'\n    });\n  });\n}\n\nconst $msClient = new Multisocket(false, 3000, 'http://localhost:3000');\nConfigure_client: {\n  $msClient.client.register('response', (data) => {\n    console.log('Cliente recibió:', data);\n  });\n}\n\nHello: {\n  $msClient.client.send({\n    type: 'test',\n    message: '¡Hola servidor!'\n  });\n}\n```\n\n## API\n\n```js\nclass MultisocketClient {\n\n  constructor(multisocket) {\n    this.multisocket = multisocket;\n    this.events = {};\n  }\n\n  send(data, headers = {}) {\n    this.multisocket.socketClient.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on client of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem && eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher => dispatcher.call(this, eventItem.data));\n        }\n      }\n    }\n  }\n\n}\n\nclass MultisocketServer {\n\n  constructor(multisocket) {\n    this.multisocket = multisocket;\n    this.events = {};\n  }\n\n  send(data, headers = {}) {\n    this.multisocket.socketClient.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem, socketClient) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on server of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem && eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher => dispatcher.call(this, eventItem.data, socketClient));\n        }\n      }\n    }\n  }\n\n}\n\nclass MultisocketServerEventClient {\n\n  constructor(multisocket, serverEventClient) {\n    this.multisocket = multisocket;\n    this.events = {};\n    this.client = serverEventClient;\n  }\n\n  send(data, headers = {}) {\n    this.client.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem, socketClient) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on server of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem && eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher => dispatcher.call(this, eventItem.data, socketClient));\n        }\n      }\n    }\n  }\n\n}\n\nclass Multisocket {\n  constructor(isServer = false, port = 3000, serverUrl = '') {\n    this.isServer = isServer;\n    this.port = port;\n    this.serverUrl = serverUrl;\n\n    // Propiedades para los objetos de servidor HTTP, servidor Socket.io y cliente Socket.io\n    this.httpServer = null;\n    this.socketServer = null;\n    this.socketClient = null;\n    this.client = new MultisocketClient(this);\n    this.server = new MultisocketServer(this);\n\n    if (this.isServer) {\n      this.startServer();\n    } else {\n      this.startClient();\n    }\n  }\n\n  // Inicia el servidor socket.io\n  startServer() {\n    this.httpServer = http.createServer((req, res) => {\n      res.writeHead(200, { 'Content-Type': 'text/plain' });\n      res.end('Servidor en ejecución');\n    });\n\n    this.socketServer = socketIo(this.httpServer);\n\n    this.socketServer.on('connection', (socket) => {\n      console.log('Cliente conectado');\n      socket.on('MultisocketEvent', (data) => {\n        console.log('Mensaje del cliente:', data);\n        const temporaryClient = new MultisocketServerEventClient(this, socket);\n        this.server.dispatch(data, temporaryClient);\n      });\n\n      socket.emit('MultisocketEvent', '¡Hola desde el servidor!');\n\n      socket.on('disconnect', () => {\n        console.log('Cliente desconectado');\n      });\n    });\n\n    this.httpServer.listen(this.port, () => {\n      console.log(`Servidor escuchando en http://localhost:${this.port}`);\n    });\n  }\n\n  // Inicia el cliente socket.io\n  startClient() {\n    this.socketClient = ioClient(this.serverUrl);\n\n    this.socketClient.on('connect', () => {\n      console.log('Conectado al servidor');\n      this.socketClient.emit('MultisocketEvent', '¡Hola desde el cliente!');\n    });\n\n    this.socketClient.on('MultisocketEvent', (data) => {\n      console.log('Mensaje del servidor:', data);\n      this.client.dispatch(data);\n    });\n\n    this.socketClient.on('connect_error', (err) => {\n      console.error('Error de conexión:', err);\n    });\n  }\n}\n\nMultisocket.default = Multisocket;\n\nmodule.exports = Multisocket;\n```","readmeFilename":"README.md"}