mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-29 18:55:40 -05:00
transform server.js into an es6 class
This commit is contained in:
parent
78ff667c1f
commit
6e399122bf
1 changed files with 63 additions and 63 deletions
126
src/server.js
126
src/server.js
|
@ -1,65 +1,65 @@
|
|||
var net = require('net')
|
||||
, EventEmitter = require('events').EventEmitter
|
||||
, util = require('util')
|
||||
, Client = require('./client')
|
||||
, states = require('./transforms/serializer').states
|
||||
;
|
||||
var net = require('net');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Client = require('./client');
|
||||
var states = require('./transforms/serializer').states;
|
||||
|
||||
class Server extends EventEmitter
|
||||
{
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.socketServer = null;
|
||||
this.cipher = null;
|
||||
this.decipher = null;
|
||||
this.clients = {};
|
||||
}
|
||||
|
||||
listen(port, host) {
|
||||
var self = this;
|
||||
var nextId = 0;
|
||||
self.socketServer = net.createServer();
|
||||
self.socketServer.on('connection', function(socket) {
|
||||
var client = new Client(true);
|
||||
client._end = client.end;
|
||||
client.end = function end(endReason) {
|
||||
endReason='{"text":"'+endReason+'"}';
|
||||
if(client.state === states.PLAY) {
|
||||
client.write('kick_disconnect', {reason: endReason});
|
||||
} else if(client.state === states.LOGIN) {
|
||||
client.write('disconnect', {reason: endReason});
|
||||
}
|
||||
client._end(endReason);
|
||||
};
|
||||
client.id = nextId++;
|
||||
self.clients[client.id] = client;
|
||||
client.on('end', function() {
|
||||
delete self.clients[client.id];
|
||||
});
|
||||
client.setSocket(socket);
|
||||
self.emit('connection', client);
|
||||
});
|
||||
self.socketServer.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
self.socketServer.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
self.socketServer.on('listening', function() {
|
||||
self.emit('listening');
|
||||
});
|
||||
self.socketServer.listen(port, host);
|
||||
}
|
||||
|
||||
close() {
|
||||
var client;
|
||||
for(var clientId in this.clients) {
|
||||
if(!this.clients.hasOwnProperty(clientId)) continue;
|
||||
|
||||
client = this.clients[clientId];
|
||||
client.end('ServerShutdown');
|
||||
}
|
||||
this.socketServer.close();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Server;
|
||||
|
||||
function Server() {
|
||||
EventEmitter.call(this);
|
||||
|
||||
this.socketServer = null;
|
||||
this.cipher = null;
|
||||
this.decipher = null;
|
||||
this.clients = {};
|
||||
}
|
||||
util.inherits(Server, EventEmitter);
|
||||
|
||||
Server.prototype.listen = function(port, host) {
|
||||
var self = this;
|
||||
var nextId = 0;
|
||||
self.socketServer = net.createServer();
|
||||
self.socketServer.on('connection', function(socket) {
|
||||
var client = new Client(true);
|
||||
client._end = client.end;
|
||||
client.end = function end(endReason) {
|
||||
endReason='{"text":"'+endReason+'"}';
|
||||
if(client.state === states.PLAY) {
|
||||
client.write('kick_disconnect', {reason: endReason});
|
||||
} else if(client.state === states.LOGIN) {
|
||||
client.write('disconnect', {reason: endReason});
|
||||
}
|
||||
client._end(endReason);
|
||||
};
|
||||
client.id = nextId++;
|
||||
self.clients[client.id] = client;
|
||||
client.on('end', function() {
|
||||
delete self.clients[client.id];
|
||||
});
|
||||
client.setSocket(socket);
|
||||
self.emit('connection', client);
|
||||
});
|
||||
self.socketServer.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
self.socketServer.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
self.socketServer.on('listening', function() {
|
||||
self.emit('listening');
|
||||
});
|
||||
self.socketServer.listen(port, host);
|
||||
};
|
||||
|
||||
Server.prototype.close = function() {
|
||||
var client;
|
||||
for(var clientId in this.clients) {
|
||||
if(!this.clients.hasOwnProperty(clientId)) continue;
|
||||
|
||||
client = this.clients[clientId];
|
||||
client.end('ServerShutdown');
|
||||
}
|
||||
this.socketServer.close();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue