io.js 3.3 KB

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