index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Module dependencies.
  3. */
  4. var http = require('http')
  5. , engine = require('engine.io')
  6. , Client = require('./client')
  7. , Namespace = require('./namespace')
  8. , debug = require('debug')('socket.io:server');
  9. /**
  10. * Read client
  11. */
  12. var client = {
  13. source: require('socket.io-client').source,
  14. version: require('socket.io-client/package').version
  15. };
  16. /**
  17. * Server constructor.
  18. *
  19. * @param {http.Server|Number|Object} http server, port or options
  20. * @param {Object} options
  21. * @api public
  22. */
  23. function Server(srv, opts){
  24. if (!(this instanceof Server)) return new Server(srv, opts);
  25. if ('object' == typeof srv && !srv.listen) {
  26. opts = srv;
  27. srv = null;
  28. }
  29. opts = opts || {};
  30. if (srv) this.attach(srv, opts);
  31. this.sockets = this.of('/');
  32. this.client(true);
  33. }
  34. /**
  35. * Serve client code.
  36. *
  37. * @api public
  38. */
  39. /**
  40. * Attaches socket.io to a server or port.
  41. *
  42. * @param {http.Server|Number} server or port
  43. * @param {Object} options
  44. * @api public
  45. */
  46. Server.prototype.attach = function(srv, opts){
  47. if ('number' == typeof srv) {
  48. debug('creating engine.io on port %d', srv);
  49. srv = engine.listen(srv, opts);
  50. } else {
  51. debug('creating engine.io instance', opts);
  52. srv = engine.attach(srv, opts);
  53. }
  54. this.bind(srv);
  55. };
  56. /**
  57. * Binds socket.io to an engine.io instance.
  58. *
  59. * @param {engine.Server} engine.io (or compatible) server
  60. * @api public
  61. */
  62. Server.prototype.bind = function(engine){
  63. this.engine = engine;
  64. this.engine.on('connection', this.onconnection.bind(this));
  65. };
  66. /**
  67. * Called with each incoming transport connection.
  68. *
  69. * @api public
  70. */
  71. Server.prototype.onconnection = function(conn){
  72. debug('incoming connection with id %s', conn.id);
  73. var client = new Client(this, conn);
  74. client.connect('/');
  75. this.emit('client', client);
  76. };
  77. /**
  78. * Looks up a namespace.
  79. *
  80. * @param {String} nsp name
  81. * @api public
  82. */
  83. Server.prototype.of = function(name){
  84. if (!this.nsps[name]) {
  85. debug('initializing namespace %s', name);
  86. var nsp = new Namespace(this, name);
  87. this.nsps[name] = nsp;
  88. }
  89. return this.nsps[name];
  90. };
  91. /**
  92. * Expose main namespace (/).
  93. */
  94. ['use', 'to', 'in', 'emit', 'send', 'write'].forEach(function(name){
  95. Server.prototype[name] = function(){
  96. var nsp = this.sockets[name];
  97. return nsp.apply(this.sockets, arguments);
  98. };
  99. });
  100. Namespace.flags.forEach(function(flag){
  101. Server.prototype.__defineGetter__(flag, function(name){
  102. this.flags.push(name);
  103. return this;
  104. });
  105. });
  106. /**
  107. * BC with `io.listen`
  108. */
  109. Server.listen = Server;