Update to use compatible sync API (createClient)

This commit is contained in:
deathcap 2016-01-30 22:14:13 -08:00
parent 00bf6acea9
commit 66e6f4dd0b
3 changed files with 22 additions and 27 deletions

View File

@ -75,10 +75,9 @@ Returns a `Client` instance and perform login.
* checkTimeoutInterval : default to `10*1000` (10s), check if keepalive received at that period, disconnect otherwise. * checkTimeoutInterval : default to `10*1000` (10s), check if keepalive received at that period, disconnect otherwise.
* version : 1.8 or 1.9 : default to 1.8 * version : 1.8 or 1.9 : default to 1.8
## mc.createClientAuto(options, cb) ## mc.createClientAuto(options)
Pings the server and attempts to call `createClient(options)` with the appropriate protocol version. Pings the server and attempts to call `createClient(options)` with the appropriate protocol version.
When created, calls the callback `cb(err, client)`.
## mc.Client(isServer,version) ## mc.Client(isServer,version)

View File

@ -5,13 +5,12 @@ if(process.argv.length < 4 || process.argv.length > 6) {
process.exit(1); process.exit(1);
} }
mc.createClientAuto({ var client = mc.createClientAuto({
host: process.argv[2], host: process.argv[2],
port: parseInt(process.argv[3]), port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : "echo", username: process.argv[4] ? process.argv[4] : "echo",
password: process.argv[5] password: process.argv[5]
}, function(err, client) { });
if (err) throw err;
client.on('connect', function() { client.on('connect', function() {
console.info('connected'); console.info('connected');
@ -28,4 +27,3 @@ mc.createClientAuto({
client.write('chat', {message: msg}); client.write('chat', {message: msg});
} }
}); });
});

View File

@ -16,7 +16,7 @@ function protocol2version(n) {
throw new Error(`unsupported/unknown protocol version: ${versionProtocol}, update protocol2version`); throw new Error(`unsupported/unknown protocol version: ${versionProtocol}, update protocol2version`);
} }
function createClientAsync(options, cb) { function createClientAuto(options) {
assert.ok(options, 'options is required'); assert.ok(options, 'options is required');
debug('creating client'); debug('creating client');
@ -26,7 +26,7 @@ function createClientAsync(options, cb) {
// TODO: refactor with DNS SRV lookup in NMP // TODO: refactor with DNS SRV lookup in NMP
// TODO: detect ping timeout, https://github.com/PrismarineJS/node-minecraft-protocol/issues/329 // TODO: detect ping timeout, https://github.com/PrismarineJS/node-minecraft-protocol/issues/329
ping(options, function(err, response) { ping(options, function(err, response) {
if (err) return cb(err, null); if (err) throw err; // hmm
debug('ping response',response); debug('ping response',response);
// TODO: could also use ping pre-connect to save description, type, negotiate protocol etc. // TODO: could also use ping pre-connect to save description, type, negotiate protocol etc.
// ^ see https://github.com/PrismarineJS/node-minecraft-protocol/issues/327 // ^ see https://github.com/PrismarineJS/node-minecraft-protocol/issues/327
@ -61,10 +61,8 @@ function createClientAsync(options, cb) {
} }
// done configuring client object, let connection proceed // done configuring client object, let connection proceed
client.emit('connect_allowed'); client.emit('connect_allowed');
cb(null, client);
}); });
return client;
} }
module.exports = createClientAsync; module.exports = createClientAuto;