From 66e6f4dd0b2549707398a2ea32675776ec5c330a Mon Sep 17 00:00:00 2001 From: deathcap Date: Sat, 30 Jan 2016 22:14:13 -0800 Subject: [PATCH] Update to use compatible sync API (createClient) --- doc/README.md | 3 +-- examples/client_auto/client_auto.js | 36 ++++++++++++++--------------- src/createClientAuto.js | 10 ++++---- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/doc/README.md b/doc/README.md index c109899..68b54b7 100644 --- a/doc/README.md +++ b/doc/README.md @@ -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. * 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. -When created, calls the callback `cb(err, client)`. ## mc.Client(isServer,version) diff --git a/examples/client_auto/client_auto.js b/examples/client_auto/client_auto.js index 0af381f..99701ae 100644 --- a/examples/client_auto/client_auto.js +++ b/examples/client_auto/client_auto.js @@ -5,27 +5,25 @@ if(process.argv.length < 4 || process.argv.length > 6) { process.exit(1); } -mc.createClientAuto({ +var client = mc.createClientAuto({ host: process.argv[2], port: parseInt(process.argv[3]), username: process.argv[4] ? process.argv[4] : "echo", password: process.argv[5] -}, function(err, client) { - if (err) throw err; - - client.on('connect', function() { - console.info('connected'); - }); - client.on('disconnect', function(packet) { - console.log('disconnected: '+ packet.reason); - }); - client.on('chat', function(packet) { - var jsonMsg = JSON.parse(packet.message); - if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') { - var username = jsonMsg.with[0].text; - var msg = jsonMsg.with[1]; - if(username === client.username) return; - client.write('chat', {message: msg}); - } - }); +}); + +client.on('connect', function() { + console.info('connected'); +}); +client.on('disconnect', function(packet) { + console.log('disconnected: '+ packet.reason); +}); +client.on('chat', function(packet) { + var jsonMsg = JSON.parse(packet.message); + if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') { + var username = jsonMsg.with[0].text; + var msg = jsonMsg.with[1]; + if(username === client.username) return; + client.write('chat', {message: msg}); + } }); diff --git a/src/createClientAuto.js b/src/createClientAuto.js index dc9fbd1..95c5ab6 100644 --- a/src/createClientAuto.js +++ b/src/createClientAuto.js @@ -16,7 +16,7 @@ function protocol2version(n) { throw new Error(`unsupported/unknown protocol version: ${versionProtocol}, update protocol2version`); } -function createClientAsync(options, cb) { +function createClientAuto(options) { assert.ok(options, 'options is required'); debug('creating client'); @@ -26,7 +26,7 @@ function createClientAsync(options, cb) { // TODO: refactor with DNS SRV lookup in NMP // TODO: detect ping timeout, https://github.com/PrismarineJS/node-minecraft-protocol/issues/329 ping(options, function(err, response) { - if (err) return cb(err, null); + if (err) throw err; // hmm debug('ping response',response); // TODO: could also use ping pre-connect to save description, type, negotiate protocol etc. // ^ 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 client.emit('connect_allowed'); - - - cb(null, client); }); + return client; } -module.exports = createClientAsync; +module.exports = createClientAuto;