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.
* 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)

View File

@ -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});
}
});

View File

@ -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;