socketHandler.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. const passport = require('passport');
  3. module.exports = (core, io, app) => {
  4. io.on('connection', socket => {
  5. console.log("User has connected");
  6. let _user = socket.request.user;
  7. let _currentStation = "";
  8. socket.on('disconnect', _ => {
  9. core['/stations/leave/:id'](_currentStation);
  10. _currentStation = "";
  11. console.log('User has disconnected');
  12. });
  13. socket.on('/users/logout', _ => {
  14. app.use((req, res) => {
  15. req.logout();
  16. });
  17. });
  18. socket.on('error', err => {
  19. console.log(err);
  20. });
  21. socket.on('/stations/join/:id', (id, cb) => {
  22. core['/stations/join/:id'](id, result => {
  23. _currentStation = id;
  24. cb(result);
  25. });
  26. });
  27. socket.on('/stations/leave/:id', (id, cb) => {
  28. core['/stations/leave/:id'](id, result => {
  29. _currentStation = "";
  30. cb(result);
  31. });
  32. })
  33. socket.on('/youtube/getVideo/:query', (query, cb) => {
  34. core['/youtube/getVideo/:query'](query, result => {
  35. cb(JSON.parse(result));
  36. });
  37. });
  38. socket.on('/stations/add/:song', (station, song, cb) => {
  39. core['/stations/add/:song'](station, song, _user, result => {
  40. cb(result);
  41. });
  42. });
  43. socket.on('/songs', (cb) => {
  44. core['/songs'](result => {
  45. cb(result);
  46. });
  47. });
  48. socket.on('/stations', (cb) => {
  49. core['/stations'](result => {
  50. cb(result);
  51. });
  52. });
  53. socket.on('/songs/:song/update', (song, cb) => {
  54. core['/songs/:song/update'](song, result => {
  55. cb(result);
  56. });
  57. });
  58. socket.on('/songs/:song/remove', (song, cb) => {
  59. core['/songs/:song/remove'](song, result => {
  60. cb(result);
  61. });
  62. });
  63. // this lets the client socket know that they can start making request
  64. console.log(socket.request.user);
  65. socket.emit('ready', socket.request.user.logged_in);
  66. });
  67. };