io.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 = ["app", "db", "cache", "utils"];
  13. }*/
  14. initialize() {
  15. return new Promise(resolve => {
  16. this.setStage(1);
  17. const app = express();
  18. const server = http.createServer(app);
  19. const io = socketio(server);
  20. this.handlers = {
  21. "getAccounts": cb => {
  22. cb({
  23. accounts: []
  24. });
  25. }
  26. }
  27. io.on('connection', (socket) => {
  28. console.log('a user connected');
  29. Object.keys(this.handlers).forEach(handlerName => {
  30. socket.on(handlerName, (...args) => {
  31. let cb = args[args.length - 1];
  32. if (typeof cb !== "function")
  33. cb = () => {
  34. this.logger.info("IO_MODULE", `There was no callback provided for ${name}.`);
  35. }
  36. else args.pop();
  37. this.handlers[handlerName].apply(null, [cb].concat(args));
  38. });
  39. });
  40. });
  41. server.listen(8080, function(){
  42. console.log('listening on *:8080');
  43. resolve();
  44. });
  45. });
  46. }
  47. }