mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-12-02 03:56:54 -05:00
transform client.js into an es6 class
This commit is contained in:
parent
2c93a4e796
commit
4e9d8e06c9
1 changed files with 145 additions and 145 deletions
|
@ -1,21 +1,20 @@
|
||||||
var EventEmitter = require('events').EventEmitter
|
var EventEmitter = require('events').EventEmitter;
|
||||||
, util = require('util')
|
var debug = require('./debug');
|
||||||
, debug = require('./debug')
|
var serializer = require('./transforms/serializer');
|
||||||
, serializer = require('./transforms/serializer')
|
var compression = require('./transforms/compression');
|
||||||
, compression = require('./transforms/compression')
|
var framing = require('./transforms/framing');
|
||||||
, framing = require('./transforms/framing')
|
var crypto = require('crypto');
|
||||||
, crypto = require('crypto')
|
var states = serializer.states;
|
||||||
, states = serializer.states
|
|
||||||
;
|
|
||||||
var version = require('./version');
|
var version = require('./version');
|
||||||
var packets = require('minecraft-data')(version.majorVersion).protocol.states;
|
var packets = require('minecraft-data')(version.majorVersion).protocol.states;
|
||||||
var readPackets = require("./packets").readPackets;
|
var readPackets = require("./packets").readPackets;
|
||||||
var packetIndexes = readPackets(packets, states);
|
var packetIndexes = readPackets(packets, states);
|
||||||
|
|
||||||
module.exports = Client;
|
|
||||||
|
|
||||||
function Client(isServer) {
|
class Client extends EventEmitter
|
||||||
EventEmitter.call(this);
|
{
|
||||||
|
constructor(isServer) {
|
||||||
|
super();
|
||||||
|
|
||||||
var socket;
|
var socket;
|
||||||
this.packetsToParse = {};
|
this.packetsToParse = {};
|
||||||
|
@ -62,11 +61,9 @@ function Client(isServer) {
|
||||||
this.packetsToParse[event] -= 1;
|
this.packetsToParse[event] -= 1;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(Client, EventEmitter);
|
setSocket(socket) {
|
||||||
|
|
||||||
Client.prototype.setSocket = function(socket) {
|
|
||||||
var ended = false;
|
var ended = false;
|
||||||
|
|
||||||
// TODO : A lot of other things needs to be done.
|
// 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.metadata.name, parsed.buffer, parsed.metadata);
|
||||||
this.emit('raw', parsed.buffer, parsed.metadata);
|
this.emit('raw', parsed.buffer, parsed.metadata);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
Client.prototype.end = function(reason) {
|
end(reason) {
|
||||||
this._endReason = reason;
|
this._endReason = reason;
|
||||||
this.socket.end();
|
this.socket.end();
|
||||||
};
|
}
|
||||||
|
|
||||||
Client.prototype.setEncryption = function(sharedSecret) {
|
setEncryption(sharedSecret) {
|
||||||
if (this.cipher != null)
|
if (this.cipher != null)
|
||||||
throw new Error("Set encryption twice !");
|
throw new Error("Set encryption twice !");
|
||||||
this.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
|
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.decipher.on('error', (err) => this.emit('error', err));
|
||||||
this.socket.unpipe(this.splitter);
|
this.socket.unpipe(this.splitter);
|
||||||
this.socket.pipe(this.decipher).pipe(this.splitter);
|
this.socket.pipe(this.decipher).pipe(this.splitter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Client.prototype.setCompressionThreshold = function(threshold) {
|
setCompressionThreshold(threshold) {
|
||||||
if (this.compressor == null) {
|
if (this.compressor == null) {
|
||||||
this.compressor = compression.createCompressor(threshold);
|
this.compressor = compression.createCompressor(threshold);
|
||||||
this.compressor.on('error', (err) => this.emit('error', err));
|
this.compressor.on('error', (err) => this.emit('error', err));
|
||||||
|
@ -145,17 +142,20 @@ Client.prototype.setCompressionThreshold = function(threshold) {
|
||||||
this.decompressor.threshold = threshold;
|
this.decompressor.threshold = threshold;
|
||||||
this.compressor.threshold = threshold;
|
this.compressor.threshold = threshold;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Client.prototype.write = function(packetName, params) {
|
write(packetName, params) {
|
||||||
debug("writing packet " + this.state + "." + packetName);
|
debug("writing packet " + this.state + "." + packetName);
|
||||||
debug(params);
|
debug(params);
|
||||||
this.serializer.write({ packetName, params });
|
this.serializer.write({ packetName, params });
|
||||||
};
|
}
|
||||||
|
|
||||||
Client.prototype.writeRaw = function(buffer) {
|
writeRaw(buffer) {
|
||||||
if (this.compressor === null)
|
if (this.compressor === null)
|
||||||
this.framer.write(buffer);
|
this.framer.write(buffer);
|
||||||
else
|
else
|
||||||
this.compressor.write(buffer);
|
this.compressor.write(buffer);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Client;
|
||||||
|
|
Loading…
Reference in a new issue