server-tool-listener.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. import io from 'socket.io';
  3. export default class ServerToolListner {
  4. constructor(server) {
  5. this.server=server;
  6. }
  7. listen(port) {
  8. let server = this.server;
  9. this.serverTool = io.listen(port);
  10. this.serverTool.on('connection', function (socket) {
  11. console.log('tool connected');
  12. socket.on('players', (option,callback) => {
  13. callback(server.getCurrentListAuthorizedPlayers());
  14. socket.disconnect(false);
  15. });
  16. socket.on('syncdb', async (option,callback) => {
  17. let response = await server.updatePlayersFromDb();
  18. callback(response);
  19. socket.disconnect(false);
  20. });
  21. socket.on('add-player', async (username,callback) => {
  22. let response = await server.addPlayerDb(...username);
  23. callback(response);
  24. socket.disconnect(false);
  25. });
  26. socket.on('remove-player', async (username,callback) => {
  27. let response = await server.removePlayerDb(...username);
  28. callback(response);
  29. socket.disconnect(false);
  30. });
  31. socket.on('add-game', async (game,callback) => {
  32. let response = await server.addGameDb(...game);
  33. callback(response);
  34. socket.disconnect(false);
  35. });
  36. socket.on('remove-game', async (options,callback) => {
  37. let response = await server.removeGameDb(...options);
  38. callback(response);
  39. socket.disconnect(false);
  40. });
  41. });
  42. }
  43. }