socketHandler.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('error', err => {
  13. console.log(err);
  14. });
  15. socket.on('/stations/join/:id', (id, cb) => {
  16. core['/stations/join/:id'](id, result => {
  17. _currentStation = id;
  18. cb(result);
  19. });
  20. });
  21. socket.on('/stations/leave/:id', (id, cb) => {
  22. core['/stations/leave/:id'](id, result => {
  23. _currentStation = "";
  24. cb(result);
  25. });
  26. });
  27. socket.on('/youtube/getVideo/:query', (query, cb) => {
  28. core['/youtube/getVideo/:query'](query, result => {
  29. cb(JSON.parse(result));
  30. });
  31. });
  32. socket.on('/stations/add/:song', (station, song, cb) => {
  33. core['/stations/add/:song'](station, song, _user, result => {
  34. cb(result);
  35. });
  36. });
  37. socket.on('/songs', (cb) => {
  38. core['/songs'](result => {
  39. cb(result[0]);
  40. });
  41. });
  42. socket.on('/stations', (cb) => {
  43. core['/stations'](result => {
  44. cb(result);
  45. });
  46. });
  47. socket.on('/songs/update', (songs, cb) => {
  48. core['/songs/update'](songs, result => {
  49. cb(result);
  50. });
  51. });
  52. // this lets the client socket know that they can start making request
  53. socket.emit('ready', socket.request.user.logged_in);
  54. });
  55. };