server.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Component for server starup.
  3. */
  4. var Server = require('../server/server');
  5. /**
  6. * Component factory function
  7. *
  8. * @param {Object} app current application context
  9. * @return {Object} component instance
  10. */
  11. module.exports = function(app, opts) {
  12. return new Component(app, opts);
  13. };
  14. /**
  15. * Server component class
  16. *
  17. * @param {Object} app current application context
  18. */
  19. var Component = function(app, opts) {
  20. this.server = Server.create(app, opts);
  21. };
  22. var pro = Component.prototype;
  23. pro.name = '__server__';
  24. /**
  25. * Component lifecycle callback
  26. *
  27. * @param {Function} cb
  28. * @return {Void}
  29. */
  30. pro.start = function(cb) {
  31. this.server.start();
  32. process.nextTick(cb);
  33. };
  34. /**
  35. * Component lifecycle callback
  36. *
  37. * @param {Function} cb
  38. * @return {Void}
  39. */
  40. Component.prototype.afterStart = function(cb) {
  41. this.server.afterStart();
  42. process.nextTick(cb);
  43. };
  44. /**
  45. * Component lifecycle function
  46. *
  47. * @param {Boolean} force whether stop the component immediately
  48. * @param {Function} cb
  49. * @return {Void}
  50. */
  51. pro.stop = function(force, cb) {
  52. this.server.stop();
  53. process.nextTick(cb);
  54. };
  55. /**
  56. * Proxy server handle
  57. */
  58. pro.handle = function(msg, session, cb) {
  59. this.server.handle(msg, session, cb);
  60. };
  61. /**
  62. * Proxy server global handle
  63. */
  64. Component.prototype.globalHandle = function(msg, session, cb) {
  65. this.server.globalHandle(msg, session, cb);
  66. };