server.js 963 B

12345678910111213141516171819202122232425262728293031323334
  1. var net = require('net');
  2. var util = require('util');
  3. var EventEmitter = require('events').EventEmitter;
  4. var Connection = require('./connection');
  5. var ConnectionConfig = require('./connection_config');
  6. // TODO: inherit Server from net.Server
  7. function Server()
  8. {
  9. EventEmitter.call(this);
  10. this.connections = [];
  11. this._server = net.createServer(this._handleConnection.bind(this));
  12. }
  13. util.inherits(Server, EventEmitter);
  14. Server.prototype._handleConnection = function(socket) {
  15. var connectionConfig = new ConnectionConfig({ stream: socket, isServer: true});
  16. var connection = new Connection({ config: connectionConfig});
  17. this.emit('connection', connection);
  18. this.connections.push(connection);
  19. };
  20. Server.prototype.listen = function(port, host, backlog, callback) {
  21. this._server.listen.apply(this._server, arguments);
  22. return this;
  23. };
  24. Server.prototype.close = function(cb) {
  25. this._server.close(cb);
  26. };
  27. module.exports = Server;