transform client.js into an es6 class

This commit is contained in:
Romain Beaumont 2015-09-20 21:12:56 +02:00
parent 2c93a4e796
commit 4e9d8e06c9

View file

@ -1,21 +1,20 @@
var EventEmitter = require('events').EventEmitter
, util = require('util')
, debug = require('./debug')
, serializer = require('./transforms/serializer')
, compression = require('./transforms/compression')
, framing = require('./transforms/framing')
, crypto = require('crypto')
, states = serializer.states
;
var EventEmitter = require('events').EventEmitter;
var debug = require('./debug');
var serializer = require('./transforms/serializer');
var compression = require('./transforms/compression');
var framing = require('./transforms/framing');
var crypto = require('crypto');
var states = serializer.states;
var version = require('./version');
var packets = require('minecraft-data')(version.majorVersion).protocol.states;
var readPackets = require("./packets").readPackets;
var packetIndexes = readPackets(packets, states);
module.exports = Client;
function Client(isServer) {
EventEmitter.call(this);
class Client extends EventEmitter
{
constructor(isServer) {
super();
var socket;
this.packetsToParse = {};
@ -62,11 +61,9 @@ function Client(isServer) {
this.packetsToParse[event] -= 1;
}
});
}
}
util.inherits(Client, EventEmitter);
Client.prototype.setSocket = function(socket) {
setSocket(socket) {
var ended = false;
// TODO : A lot of other things needs to be done.
@ -111,14 +108,14 @@ Client.prototype.setSocket = function(socket) {
this.emit('raw.' + parsed.metadata.name, parsed.buffer, parsed.metadata);
this.emit('raw', parsed.buffer, parsed.metadata);
});
};
}
Client.prototype.end = function(reason) {
end(reason) {
this._endReason = reason;
this.socket.end();
};
}
Client.prototype.setEncryption = function(sharedSecret) {
setEncryption(sharedSecret) {
if (this.cipher != null)
throw new Error("Set encryption twice !");
this.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
@ -129,9 +126,9 @@ Client.prototype.setEncryption = function(sharedSecret) {
this.decipher.on('error', (err) => this.emit('error', err));
this.socket.unpipe(this.splitter);
this.socket.pipe(this.decipher).pipe(this.splitter);
}
}
Client.prototype.setCompressionThreshold = function(threshold) {
setCompressionThreshold(threshold) {
if (this.compressor == null) {
this.compressor = compression.createCompressor(threshold);
this.compressor.on('error', (err) => this.emit('error', err));
@ -145,17 +142,20 @@ Client.prototype.setCompressionThreshold = function(threshold) {
this.decompressor.threshold = threshold;
this.compressor.threshold = threshold;
}
}
}
Client.prototype.write = function(packetName, params) {
write(packetName, params) {
debug("writing packet " + this.state + "." + packetName);
debug(params);
this.serializer.write({ packetName, params });
};
}
Client.prototype.writeRaw = function(buffer) {
writeRaw(buffer) {
if (this.compressor === null)
this.framer.write(buffer);
else
this.compressor.write(buffer);
};
}
}
module.exports = Client;