io.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. // This file contains all the logic for Socket.IO
  3. const app = require('./app');
  4. const actions = require('./actions');
  5. const cache = require('./cache');
  6. const utils = require('./utils');
  7. const db = require('./db');
  8. module.exports = {
  9. io: null,
  10. init: (cb) => {
  11. //TODO Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
  12. this.io = require('socket.io')(app.server);
  13. this.io.use((socket, next) => {
  14. let cookies = socket.request.headers.cookie;
  15. let SID = utils.cookies.parseCookies(cookies).SID;
  16. if (!SID) SID = "NONE";
  17. cache.hget('userSessions', SID, (err, userSession) => {
  18. if (err) SID = null;
  19. let sessionId = utils.guid();
  20. cache.hset('sessions', sessionId, cache.schemas.session(SID), (err) => {
  21. socket.sessionId = sessionId;
  22. return next();
  23. });
  24. });
  25. });
  26. this.io.on('connection', socket => {
  27. console.info('User has connected');
  28. // catch when the socket has been disconnected
  29. socket.on('disconnect', () => {
  30. // remove the user from their current station (if any)
  31. if (socket.sessionId) {
  32. //actions.stations.leave(socket.sessionId, result => {});
  33. // Remove session from Redis
  34. cache.hdel('sessions', socket.sessionId);
  35. }
  36. console.info('User has disconnected');
  37. });
  38. // catch errors on the socket (internal to socket.io)
  39. socket.on('error', err => console.error(err));
  40. // have the socket listen for each action
  41. Object.keys(actions).forEach((namespace) => {
  42. Object.keys(actions[namespace]).forEach((action) => {
  43. // the full name of the action
  44. let name = `${namespace}.${action}`;
  45. // listen for this action to be called
  46. socket.on(name, function () {
  47. let args = Array.prototype.slice.call(arguments, 0, -1);
  48. let cb = arguments[arguments.length - 1];
  49. // load the session from the cache
  50. cache.hget('sessions', socket.sessionId, (err, session) => {
  51. if (err && err !== true) {
  52. if (typeof cb === 'function') return cb({
  53. status: 'error',
  54. message: 'An error occurred while obtaining your session'
  55. });
  56. }
  57. // make sure the sockets sessionId isn't set if there is no session
  58. if (socket.sessionId && session === null) delete socket.sessionId;
  59. // call the action, passing it the session, and the arguments socket.io passed us
  60. actions[namespace][action].apply(null, [socket.sessionId].concat(args).concat([
  61. (result) => {
  62. // respond to the socket with our message
  63. if (typeof cb === 'function') return cb(result);
  64. }
  65. ]));
  66. });
  67. })
  68. })
  69. });
  70. //TODO check if session is valid before returning true/false
  71. if (socket.sessionId !== undefined) cache.hget('sessions', socket.sessionId, (err, session) => {
  72. if (err && err !== true) socket.emit('ready', false);
  73. else if (session) {
  74. if (!!session.userSessionId) {
  75. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  76. if (err && err !== true) socket.emit('ready', false);
  77. else if (userSession) {
  78. db.models.user.findOne({ _id: userSession.userId }, (err, user) => {
  79. let role = '';
  80. let username = '';
  81. if (user) {
  82. role = user.role;
  83. username = user.username;
  84. }
  85. socket.emit('ready', true, role, username);
  86. });
  87. } else socket.emit('ready', false);
  88. });
  89. } else socket.emit('ready', false);
  90. } else socket.emit('ready', false);
  91. });
  92. });
  93. cb();
  94. }
  95. };