socketHandler.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. module.exports = (core, io) => {
  3. io.on('connection', socket => {
  4. console.log("User has connected");
  5. let _user = socket.request.user;
  6. let _currentStation = "";
  7. socket.on('disconnect', () => {
  8. core['/stations/leave/:id'](_currentStation);
  9. _currentStation = "";
  10. console.log('User has disconnected');
  11. });
  12. socket.on('/stations/join/:id', (id, cb) => {
  13. core['/stations/join/:id'](id, result => {
  14. _currentStation = id;
  15. cb(result);
  16. });
  17. });
  18. socket.on('/stations/leave/:id', (id, cb) => {
  19. core['/stations/leave/:id'](id, result => {
  20. _currentStation = "";
  21. cb(result);
  22. });
  23. });
  24. socket.on('/youtube/getVideo/:query', (query, cb) => {
  25. core['/youtube/getVideo/:query'](query, result => {
  26. cb(JSON.parse(result));
  27. });
  28. });
  29. socket.on('/songs/queue/add/:song', (song, cb) => {
  30. core['/songs/queue/add/:song'](song, _user, result => {
  31. cb(result);
  32. });
  33. });
  34. socket.on('/songs/queue/getSongs', (cb) => {
  35. core['/songs/queue/getSongs'](_user, result => {
  36. cb(result);
  37. });
  38. });
  39. socket.on('/songs/queue/updateSong/:id', (id, object, cb) => {
  40. core['/songs/queue/updateSong/:id'](_user, id, object, result => {
  41. cb(result);
  42. });
  43. });
  44. // this lets the client socket know that they can start making request
  45. socket.emit('ready', socket.request.user.logged_in);
  46. });
  47. };