transform server.js into an es6 class

This commit is contained in:
Romain Beaumont 2015-09-20 21:36:59 +02:00
parent 78ff667c1f
commit 6e399122bf

View file

@ -1,23 +1,20 @@
var net = require('net') var net = require('net');
, EventEmitter = require('events').EventEmitter var EventEmitter = require('events').EventEmitter;
, util = require('util') var Client = require('./client');
, Client = require('./client') var states = require('./transforms/serializer').states;
, states = require('./transforms/serializer').states
;
module.exports = Server; class Server extends EventEmitter
{
function Server() { constructor() {
EventEmitter.call(this); super();
this.socketServer = null; this.socketServer = null;
this.cipher = null; this.cipher = null;
this.decipher = null; this.decipher = null;
this.clients = {}; this.clients = {};
} }
util.inherits(Server, EventEmitter);
Server.prototype.listen = function(port, host) { listen(port, host) {
var self = this; var self = this;
var nextId = 0; var nextId = 0;
self.socketServer = net.createServer(); self.socketServer = net.createServer();
@ -51,9 +48,9 @@ Server.prototype.listen = function(port, host) {
self.emit('listening'); self.emit('listening');
}); });
self.socketServer.listen(port, host); self.socketServer.listen(port, host);
}; }
Server.prototype.close = function() { close() {
var client; var client;
for(var clientId in this.clients) { for(var clientId in this.clients) {
if(!this.clients.hasOwnProperty(clientId)) continue; if(!this.clients.hasOwnProperty(clientId)) continue;
@ -62,4 +59,7 @@ Server.prototype.close = function() {
client.end('ServerShutdown'); client.end('ServerShutdown');
} }
this.socketServer.close(); this.socketServer.close();
}; }
}
module.exports = Server;