Add wait_connect option, to require 'connect_allowed' event before src/client/setProtocol proceeds

Intended to allow clients to configure the client object before any data
is sent, specifically, before src/client/setProtocol sends the
set_protocol packet.
This commit is contained in:
deathcap 2016-01-28 09:49:35 -08:00
parent a53a2977f2
commit 9ec6d876ba
2 changed files with 18 additions and 4 deletions

View File

@ -5,6 +5,14 @@ module.exports = function(client, options) {
client.on('connect', onConnect);
function onConnect() {
if (options.wait_connect) {
client.on('connect_allowed', next);
} else {
next();
}
}
function next() {
client.write('set_protocol', {
protocolVersion: options.protocolVersion,
serverHost: options.host,
@ -16,6 +24,4 @@ module.exports = function(client, options) {
username: client.username
});
}
}

View File

@ -19,6 +19,9 @@ function protocol2version(n) {
function createClientAsync(options, cb) {
assert.ok(options, 'options is required');
debug('creating client');
options.wait_connect = true; // don't let createClient / src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
var client = createClient(options); // vanilla
debug('pinging',options.host);
// TODO: refactor with DNS SRV lookup in NMP
// TODO: detect ping timeout, https://github.com/PrismarineJS/node-minecraft-protocol/issues/329
@ -41,10 +44,12 @@ function createClientAsync(options, cb) {
// Note that versionName is a descriptive version stirng like '1.8.9' on vailla, but other
// servers add their own name (Spigot 1.8.8, Glowstone++ 1.8.9) so we cannot use it directly,
// even though it is in a format accepted by minecraft-data. Instead, translate the protocol.
//XXX TODO: modify client object
options.version = protocol2version(versionProtocol);
// Use the exact same protocol version
// Requires https://github.com/PrismarineJS/node-minecraft-protocol/pull/330
//XXX TODO: modify client objecti
options.protocolVersion = versionProtocol;
if (response.modinfo && response.modinfo.type === 'FML') {
@ -53,10 +58,13 @@ function createClientAsync(options, cb) {
debug('Using forgeMods:',forgeMods);
// TODO: https://github.com/PrismarineJS/node-minecraft-protocol/issues/114
// https://github.com/PrismarineJS/node-minecraft-protocol/pull/326
// TODO: modify client object to set forgeMods and enable forgeHandshake
throw new Error('FML/Forge not yet supported');
} else {
client = createClient(options); // vanilla
}
// done configuring client object, let connection proceed
client.emit('connect_allowed');
cb(null, client);
});
}