socketHandler.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. module.exports = function (base, io) {
  3. io.on('connection', function (socket) {
  4. socket.on('disconnect', function () {
  5. base.disconnect(function () {
  6. console.log('User has disconnected');
  7. });
  8. });
  9. socket.on('login', function (user) {
  10. base.login(user, function (result) {
  11. socket.emit('login', result);
  12. });
  13. });
  14. socket.on('register', function (user) {
  15. base.register(user, function (result) {
  16. socket.emit('register', result);
  17. });
  18. });
  19. socket.on('getRooms', function () {
  20. base.rooms(function (result) {
  21. var rooms = result.map(function(result) {
  22. return {
  23. id: result.getId(),
  24. displayName: result.getDisplayName(),
  25. description: result.getDescription(),
  26. users: result.getUsers()
  27. }
  28. });
  29. socket.emit('rooms', result);
  30. });
  31. });
  32. socket.on('room', function (id, cb) {
  33. base.room(id, function (result) {
  34. var info = {
  35. displayName: result.getDisplayName(),
  36. users: result.getUsers(),
  37. currentSong: result.getCurrentSong()
  38. };
  39. cb(info);
  40. });
  41. });
  42. socket.on('search', function (query) {
  43. base.search(query, function (result) {
  44. socket.emit('search', result);
  45. });
  46. });
  47. socket.emit('ready');
  48. });
  49. };