io.js 2.7 KB

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