io.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 async = require('async');
  6. const cache = require('./cache');
  7. const utils = require('./utils');
  8. const db = require('./db');
  9. module.exports = {
  10. io: null,
  11. init: (cb) => {
  12. //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)
  13. this.io = require('socket.io')(app.server);
  14. this.io.use((socket, next) => {
  15. let cookies = socket.request.headers.cookie;
  16. let SID = utils.cookies.parseCookies(cookies).SID;
  17. async.waterfall([
  18. (next) => {
  19. if (!SID) return next('No SID.');
  20. next();
  21. },
  22. (next) => {
  23. cache.hget('sessions', SID, next);
  24. },
  25. (session, next) => {
  26. if (!session) return next('No session found.');
  27. session.refreshDate = Date.now();
  28. socket.session = session;
  29. cache.hset('sessions', SID, session, next);
  30. }
  31. ], () => {
  32. if (!socket.session) {
  33. socket.session = {socketId: socket.id};
  34. } else socket.session.socketId = socket.id;
  35. next();
  36. });
  37. });
  38. this.io.on('connection', socket => {
  39. console.info('User has connected');
  40. // catch when the socket has been disconnected
  41. socket.on('disconnect', () => {
  42. // remove the user from their current station (if any)
  43. if (socket.session) {
  44. //actions.stations.leave(socket.sessionId, result => {});
  45. // Remove session from Redis
  46. //cache.hdel('sessions', socket.session.sessionId);
  47. }
  48. console.info('User has disconnected');
  49. });
  50. // catch errors on the socket (internal to socket.io)
  51. socket.on('error', err => console.error(err));
  52. // have the socket listen for each action
  53. Object.keys(actions).forEach((namespace) => {
  54. Object.keys(actions[namespace]).forEach((action) => {
  55. // the full name of the action
  56. let name = `${namespace}.${action}`;
  57. // listen for this action to be called
  58. socket.on(name, function () {
  59. let args = Array.prototype.slice.call(arguments, 0, -1);
  60. let cb = arguments[arguments.length - 1];
  61. // load the session from the cache
  62. cache.hget('sessions', socket.session.sessionId, (err, session) => {
  63. if (err && err !== true) {
  64. if (typeof cb === 'function') return cb({
  65. status: 'error',
  66. message: 'An error occurred while obtaining your session'
  67. });
  68. }
  69. // make sure the sockets sessionId isn't set if there is no session
  70. if (socket.session.sessionId && session === null) delete socket.session.sessionId;
  71. // call the action, passing it the session, and the arguments socket.io passed us
  72. actions[namespace][action].apply(null, [socket.session].concat(args).concat([
  73. (result) => {
  74. // respond to the socket with our message
  75. if (typeof cb === 'function') return cb(result);
  76. }
  77. ]));
  78. });
  79. })
  80. })
  81. });
  82. if (socket.session.sessionId) {
  83. cache.hget('sessions', socket.session.sessionId, (err, session) => {
  84. if (err && err !== true) socket.emit('ready', false);
  85. else if (session && session.userId) {
  86. db.models.user.findOne({ _id: session.userId }, (err, user) => {
  87. if (err || !user) return socket.emit('ready', false);
  88. let role = '';
  89. let username = '';
  90. let userId = '';
  91. if (user) {
  92. role = user.role;
  93. username = user.username;
  94. userId = session.userId;
  95. }
  96. socket.emit('ready', true, role, username, userId);
  97. });
  98. } else socket.emit('ready', false);
  99. })
  100. } else socket.emit('ready', false);
  101. });
  102. cb();
  103. }
  104. };