server-tool-listener.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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='';
  33. try {
  34. response = await server.addGameDb(...game);
  35. response = "OK - inserted at id :" + response;
  36. } catch(err) {
  37. response= 'KO ' + err.message;
  38. }
  39. callback(JSON.stringify(response));
  40. socket.disconnect(false);
  41. });
  42. socket.on('remove-game', async (options,callback) => {
  43. let response = await server.removeGameDb(...options);
  44. callback(response);
  45. socket.disconnect(false);
  46. });
  47. });
  48. }
  49. }