io.js 3.6 KB

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