Add support for promises
This commit is contained in:
parent
42df8ed96b
commit
7b9fe2161c
45
README.md
45
README.md
|
@ -14,18 +14,30 @@ Usage from Node.js
|
|||
npm install gamedig
|
||||
```
|
||||
|
||||
Promise:
|
||||
```javascript
|
||||
var Gamedig = require('gamedig');
|
||||
Gamedig.query(
|
||||
{
|
||||
type: 'minecraft',
|
||||
host: 'mc.example.com'
|
||||
},
|
||||
function(state) {
|
||||
if(state.error) console.log("Server is offline");
|
||||
else console.log(state);
|
||||
}
|
||||
);
|
||||
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',
|
||||
host: 'mc.example.com'
|
||||
},
|
||||
function(e,state) {
|
||||
if(e) console.log("Server is offline");
|
||||
else console.log(state);
|
||||
});
|
||||
```
|
||||
|
||||
> 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
|
||||
* **notes**: (optional) Passed through to output
|
||||
|
||||
###Callback Function
|
||||
### Return Value
|
||||
|
||||
The callback function is "guaranteed" to be called exactly once.
|
||||
|
||||
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:
|
||||
The returned state object will contain the following keys:
|
||||
|
||||
**Stable, always present:**
|
||||
|
||||
|
@ -70,7 +77,7 @@ Some servers may return an additional player count number, which may be present
|
|||
Games List
|
||||
---
|
||||
|
||||
###Supported
|
||||
### Supported
|
||||
<!--- BEGIN GENERATED GAMES -->
|
||||
|
||||
* 7 Days to Die (7d2d) [[Separate Query Port](#separate-query-port)]
|
||||
|
@ -305,7 +312,7 @@ Games List
|
|||
|
||||
<!--- END GENERATED GAMES -->
|
||||
|
||||
###Not supported (yet)
|
||||
### Not supported (yet)
|
||||
|
||||
* rFactor Engine (rfactor):
|
||||
* rFactor
|
||||
|
|
|
@ -22,13 +22,19 @@ for(var key in argv) {
|
|||
var Gamedig = require('../lib/index');
|
||||
if(debug) Gamedig.debug = true;
|
||||
Gamedig.isCommandLine = true;
|
||||
Gamedig.query(
|
||||
options,
|
||||
function(state) {
|
||||
|
||||
Gamedig.query(options)
|
||||
.then((state) => {
|
||||
if(outputFormat == 'pretty') {
|
||||
console.log(JSON.stringify(state,null,' '));
|
||||
} else {
|
||||
console.log(JSON.stringify(state));
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
if(outputFormat == 'pretty') {
|
||||
console.log(JSON.stringify({error:error},null,' '));
|
||||
} else {
|
||||
console.log(JSON.stringify({error:error}));
|
||||
}
|
||||
});
|
||||
|
|
97
lib/index.js
97
lib/index.js
|
@ -29,50 +29,67 @@ udpSocket.on('error', function(e) {
|
|||
Gamedig = {
|
||||
|
||||
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;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
callback({error:e.message});
|
||||
});
|
||||
return;
|
||||
}
|
||||
query.debug = Gamedig.debug;
|
||||
query.udpSocket = udpSocket;
|
||||
query.type = options.type;
|
||||
|
||||
if(!('port' in query.options) && ('port_query' in query.options)) {
|
||||
if(Gamedig.isCommandLine) {
|
||||
process.stderr.write(
|
||||
"Warning! This game is so old, that we don't know"
|
||||
+" what the server's connection port is. We've guessed that"
|
||||
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
||||
+" If you know the connection port for this type of server, please let"
|
||||
+" us know on the GameDig issue tracker, thanks!\n"
|
||||
);
|
||||
var query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
options.callback({error:e.message});
|
||||
});
|
||||
return;
|
||||
}
|
||||
query.debug = Gamedig.debug;
|
||||
query.udpSocket = udpSocket;
|
||||
query.type = options.type;
|
||||
|
||||
if(!('port' in query.options) && ('port_query' in query.options)) {
|
||||
if(Gamedig.isCommandLine) {
|
||||
process.stderr.write(
|
||||
"Warning! This game is so old, that we don't know"
|
||||
+" what the server's connection port is. We've guessed that"
|
||||
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
||||
+" If you know the connection port for this type of server, please let"
|
||||
+" us know on the GameDig issue tracker, thanks!\n"
|
||||
);
|
||||
}
|
||||
query.options.port = query.options.port_query;
|
||||
delete query.options.port_query;
|
||||
}
|
||||
|
||||
// copy over options
|
||||
for(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
});
|
||||
|
||||
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}));
|
||||
}
|
||||
query.options.port = query.options.port_query;
|
||||
delete query.options.port_query;
|
||||
}
|
||||
|
||||
// copy over options
|
||||
for(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
|
||||
return query;
|
||||
return promise;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
12
package.json
12
package.json
|
@ -11,7 +11,7 @@
|
|||
],
|
||||
"main": "lib/index.js",
|
||||
"author": "Michael Morrison",
|
||||
"version": "0.2.25",
|
||||
"version": "0.2.26",
|
||||
"repository" : {
|
||||
"type" : "git",
|
||||
"url" : "https://github.com/sonicsnes/node-gamedig.git"
|
||||
|
@ -19,12 +19,10 @@
|
|||
"bugs" : {
|
||||
"url" : "https://github.com/sonicsnes/node-gamedig/issues"
|
||||
},
|
||||
"licenses" : [
|
||||
{
|
||||
"type" : "MIT",
|
||||
"url" : "https://raw.github.com/sonicsnes/node-gamedig/master/LICENSE"
|
||||
}
|
||||
],
|
||||
"license" : "MIT",
|
||||
"engines" : {
|
||||
"node" : ">=4.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"iconv-lite": "~0.4.6",
|
||||
"long": "~2.2.3",
|
||||
|
|
Loading…
Reference in New Issue