mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
Add support for promises
This commit is contained in:
parent
42df8ed96b
commit
7b9fe2161c
4 changed files with 99 additions and 71 deletions
33
README.md
33
README.md
|
|
@ -14,18 +14,30 @@ Usage from Node.js
|
||||||
npm install gamedig
|
npm install gamedig
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Promise:
|
||||||
```javascript
|
```javascript
|
||||||
var Gamedig = require('gamedig');
|
var Gamedig = require('gamedig');
|
||||||
Gamedig.query(
|
Gamedig.query({
|
||||||
{
|
type: 'minecraft',
|
||||||
|
host: 'mc.example.com'
|
||||||
|
}).then((state) => {
|
||||||
|
console.log(state);
|
||||||
|
}).catch((error) => {
|
||||||
|
console.log("Server is offline");
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
or Node.JS Callback:
|
||||||
|
```javascript
|
||||||
|
var Gamedig = require('gamedig');
|
||||||
|
Gamedig.query({
|
||||||
type: 'minecraft',
|
type: 'minecraft',
|
||||||
host: 'mc.example.com'
|
host: 'mc.example.com'
|
||||||
},
|
},
|
||||||
function(state) {
|
function(e,state) {
|
||||||
if(state.error) console.log("Server is offline");
|
if(e) console.log("Server is offline");
|
||||||
else console.log(state);
|
else console.log(state);
|
||||||
}
|
});
|
||||||
);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> Is NPM out of date? If you're feeling lucky, you can install the latest code with
|
> Is NPM out of date? If you're feeling lucky, you can install the latest code with
|
||||||
|
|
@ -40,14 +52,9 @@ Gamedig.query(
|
||||||
* **port**: (optional) Uses the protocol default if not set
|
* **port**: (optional) Uses the protocol default if not set
|
||||||
* **notes**: (optional) Passed through to output
|
* **notes**: (optional) Passed through to output
|
||||||
|
|
||||||
###Callback Function
|
### Return Value
|
||||||
|
|
||||||
The callback function is "guaranteed" to be called exactly once.
|
The returned state object will contain the following keys:
|
||||||
|
|
||||||
If an error occurs, the returned object will contain an "error" key, indicating the issue.
|
|
||||||
If the error key exists, it should be assumed that the game server is offline or unreachable.
|
|
||||||
|
|
||||||
Otherwise, the returned object is guaranteed to contain the following keys:
|
|
||||||
|
|
||||||
**Stable, always present:**
|
**Stable, always present:**
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,19 @@ for(var key in argv) {
|
||||||
var Gamedig = require('../lib/index');
|
var Gamedig = require('../lib/index');
|
||||||
if(debug) Gamedig.debug = true;
|
if(debug) Gamedig.debug = true;
|
||||||
Gamedig.isCommandLine = true;
|
Gamedig.isCommandLine = true;
|
||||||
Gamedig.query(
|
|
||||||
options,
|
Gamedig.query(options)
|
||||||
function(state) {
|
.then((state) => {
|
||||||
if(outputFormat == 'pretty') {
|
if(outputFormat == 'pretty') {
|
||||||
console.log(JSON.stringify(state,null,' '));
|
console.log(JSON.stringify(state,null,' '));
|
||||||
} else {
|
} else {
|
||||||
console.log(JSON.stringify(state));
|
console.log(JSON.stringify(state));
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
if(outputFormat == 'pretty') {
|
||||||
|
console.log(JSON.stringify({error:error},null,' '));
|
||||||
|
} else {
|
||||||
|
console.log(JSON.stringify({error:error}));
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
|
|
|
||||||
23
lib/index.js
23
lib/index.js
|
|
@ -29,14 +29,18 @@ udpSocket.on('error', function(e) {
|
||||||
Gamedig = {
|
Gamedig = {
|
||||||
|
|
||||||
query: function(options,callback) {
|
query: function(options,callback) {
|
||||||
if(callback) options.callback = callback;
|
const promise = new Promise((resolve,reject) => {
|
||||||
|
options.callback = (state) => {
|
||||||
|
if (state.error) reject(state.error);
|
||||||
|
else resolve(state);
|
||||||
|
};
|
||||||
|
|
||||||
var query;
|
var query;
|
||||||
try {
|
try {
|
||||||
query = TypeResolver.lookup(options.type);
|
query = TypeResolver.lookup(options.type);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
callback({error:e.message});
|
options.callback({error:e.message});
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -71,8 +75,21 @@ Gamedig = {
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
query.start();
|
query.start();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return query;
|
if (callback && callback instanceof Function) {
|
||||||
|
if(callback.length == 2) {
|
||||||
|
promise
|
||||||
|
.then((state) => callback(null,state))
|
||||||
|
.catch((error) => callback(error));
|
||||||
|
} else if (callback.length == 1) {
|
||||||
|
promise
|
||||||
|
.then((state) => callback(state))
|
||||||
|
.catch((error) => callback({error:error}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
12
package.json
12
package.json
|
|
@ -11,7 +11,7 @@
|
||||||
],
|
],
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"author": "Michael Morrison",
|
"author": "Michael Morrison",
|
||||||
"version": "0.2.25",
|
"version": "0.2.26",
|
||||||
"repository" : {
|
"repository" : {
|
||||||
"type" : "git",
|
"type" : "git",
|
||||||
"url" : "https://github.com/sonicsnes/node-gamedig.git"
|
"url" : "https://github.com/sonicsnes/node-gamedig.git"
|
||||||
|
|
@ -19,12 +19,10 @@
|
||||||
"bugs" : {
|
"bugs" : {
|
||||||
"url" : "https://github.com/sonicsnes/node-gamedig/issues"
|
"url" : "https://github.com/sonicsnes/node-gamedig/issues"
|
||||||
},
|
},
|
||||||
"licenses" : [
|
"license" : "MIT",
|
||||||
{
|
"engines" : {
|
||||||
"type" : "MIT",
|
"node" : ">=4.0.0"
|
||||||
"url" : "https://raw.github.com/sonicsnes/node-gamedig/master/LICENSE"
|
},
|
||||||
}
|
|
||||||
],
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"iconv-lite": "~0.4.6",
|
"iconv-lite": "~0.4.6",
|
||||||
"long": "~2.2.3",
|
"long": "~2.2.3",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue