io.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // set the sessionId for the socket (this will have to be checked every request, this allows us to have a logout all devices option)
  15. console.log(utils.cookies.parseCookies(cookies).SID);
  16. if (cookies) socket.sessionId = utils.cookies.parseCookies(cookies).SID;
  17. return next();
  18. });
  19. this.io.on('connection', socket => {
  20. socket.join("SomeRoom");
  21. console.log("io: User has connected");
  22. // catch when the socket has been disconnected
  23. socket.on('disconnect', () => {
  24. // remove the user from their current station (if any)
  25. if (socket.sessionId) {
  26. //actions.stations.leave(socket.sessionId, result => {});
  27. delete socket.sessionId;
  28. }
  29. console.log('io: User has disconnected');
  30. });
  31. // catch errors on the socket (internal to socket.io)
  32. socket.on('error', err => console.log(err));
  33. // have the socket listen for each action
  34. Object.keys(actions).forEach((namespace) => {
  35. Object.keys(actions[namespace]).forEach((action) => {
  36. // the full name of the action
  37. let name = `${namespace}.${action}`;
  38. // listen for this action to be called
  39. socket.on(name, function () {
  40. let args = Array.prototype.slice.call(arguments, 0, -1);
  41. let cb = arguments[arguments.length - 1];
  42. // load the session from the cache
  43. cache.hget('sessions', socket.sessionId, (err, session) => {
  44. if (err && err !== true) {
  45. return cb({
  46. status: 'error',
  47. message: 'An error occurred while obtaining your session'
  48. });
  49. }
  50. // make sure the sockets sessionId isn't set if there is no session
  51. if (socket.sessionId && session === null) delete socket.sessionId;
  52. // call the action, passing it the session, and the arguments socket.io passed us
  53. actions[namespace][action].apply(null, [session].concat(args).concat([
  54. (result) => {
  55. // store the session id
  56. if (name == 'users.login' && result.user) socket.sessionId = result.user.sessionId;
  57. // respond to the socket with our message
  58. cb(result);
  59. }
  60. ]));
  61. });
  62. })
  63. })
  64. });
  65. //TODO check if session is valid before returning true/false
  66. if (socket.sessionId !== undefined) cache.hget('sessions', socket.sessionId, (err, session) => {
  67. if (err && err !== true) {
  68. socket.emit('ready', false);
  69. } else if (session) {
  70. socket.emit('ready', true);
  71. } else {
  72. socket.emit('ready', false);
  73. }
  74. });
  75. });
  76. cb();
  77. }
  78. };