app.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const express = require('express');
  2. const app = express();
  3. const http = require('http');
  4. const server = http.createServer(app);
  5. const { Server, Socket } = require('socket.io');
  6. const io = new Server(server);
  7. const cors = require('cors');
  8. const path = require('path');
  9. app.use(cors());
  10. var localstorage_session = "";
  11. var users_sessions = [];
  12. app.use(express.static(path.join(__dirname, 'src')));
  13. app.get('/', (req, res) => {
  14. /*req.secure ? next() : res.redirect('https://' + req.headers.host + req.url);*/
  15. res.sendFile(__dirname + '/src/index.html');
  16. /*res.write("<h1>Prueba</h1>"); */
  17. });
  18. app.post('/', (req, res) => {
  19. /*req.secure ? next() : res.redirect('https://' + req.headers.host + req.url);*/
  20. res.sendFile(__dirname + '/src/index.html');
  21. /*res.write("<h1>Prueba</h1>"); */
  22. });
  23. io.on('connection', (socket) =>{
  24. socket.on('prueba', (msg) => {
  25. console.log('message: ', msg);
  26. });
  27. socket.on("localstorage_login", function (data) {
  28. users_sessions.push({
  29. CORREO_SESSION: data.CORREO,
  30. RFCEMPRESA_SESSION: data.RFCEMPRESA,
  31. TIMUSERENC_SESSION: data.TIMUSERENC,
  32. JWT_SESSION: data.jwt
  33. });
  34. socket.emit('localstorage_login_error', {
  35. error: false,
  36. msj: "EXITO",
  37. response: {}
  38. });
  39. });
  40. socket.on("localstorage_update", function (data) {
  41. RFCEMPRESA_UPD = data.RFCEMPRESA;
  42. TIMUSERENC_UPD = data.TIMUSERENC;
  43. JWT_UPD = data.jwt;
  44. let filter_users = users_sessions.filter(element => element.JWT_SESSION == JWT_UPD && element.TIMUSERENC_SESSION == TIMUSERENC_UPD && element.RFCEMPRESA_SESSION == RFCEMPRESA_UPD);
  45. if (filter_users.length == 0) {
  46. console.log("HUBO UN CAMBIO");
  47. socket.emit('localstorage_error', {
  48. error: true,
  49. msj: "Se detectó un cambio en el localstorage o hay más ventanas abiertas",
  50. response: {}
  51. });
  52. }else{
  53. socket.emit('localstorage_error', {
  54. error: false,
  55. msj: "EXITO",
  56. response: {}
  57. } );
  58. }
  59. });
  60. socket.on("localstorage_logout", function (data) {
  61. let index = users_sessions.findIndex( element => element.JWT_SESSION == data.JWT);
  62. users_sessions.splice(index, 1);
  63. });
  64. });
  65. server.listen(4444, () => {
  66. console.log('listening on *:4444');
  67. });