index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. // This file contains all the logic for Socket.IO
  3. const coreClass = require("../../core");
  4. const async = require("async");
  5. const config = require("config");
  6. const express = require("express");
  7. const http = require("http");
  8. const socketio = require('socket.io');
  9. module.exports = class extends coreClass {
  10. constructor(name, moduleManager) {
  11. super(name, moduleManager);
  12. this.dependsOn = ["mongo", "util"];
  13. }
  14. initialize() {
  15. return new Promise(async resolve => {
  16. this.setStage(1);
  17. const app = express();
  18. const server = http.createServer(app);
  19. const io = socketio(server);
  20. this.mongo = this.moduleManager.modules["mongo"];
  21. this.util = this.moduleManager.modules["util"];
  22. this.namespaces = require("./namespaces");
  23. io.on('connection', (socket) => {
  24. console.log('a user connected');
  25. Object.keys(this.namespaces).forEach(namespaceName => {
  26. Object.keys(this.namespaces[namespaceName]).forEach(handlerName => {
  27. socket.on(`${namespaceName}.${handlerName}`, (...args) => {
  28. let cb = args[args.length - 1];
  29. if (typeof cb !== "function")
  30. cb = () => {
  31. this.logger.info("IO_MODULE", `There was no callback provided for ${name}.`);
  32. }
  33. else args.pop();
  34. this.namespaces[namespaceName][handlerName].apply(null, [cb].concat(args));
  35. });
  36. });
  37. });
  38. });
  39. server.listen(8080, function(){
  40. console.log('listening on *:8080');
  41. resolve();
  42. });
  43. });
  44. }
  45. }