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
45
README.md
45
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',
|
||||||
type: 'minecraft',
|
host: 'mc.example.com'
|
||||||
host: 'mc.example.com'
|
}).then((state) => {
|
||||||
},
|
console.log(state);
|
||||||
function(state) {
|
}).catch((error) => {
|
||||||
if(state.error) console.log("Server is offline");
|
console.log("Server is offline");
|
||||||
else console.log(state);
|
});
|
||||||
}
|
```
|
||||||
);
|
|
||||||
|
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
|
> 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:**
|
||||||
|
|
||||||
|
|
@ -70,7 +77,7 @@ Some servers may return an additional player count number, which may be present
|
||||||
Games List
|
Games List
|
||||||
---
|
---
|
||||||
|
|
||||||
###Supported
|
### Supported
|
||||||
<!--- BEGIN GENERATED GAMES -->
|
<!--- BEGIN GENERATED GAMES -->
|
||||||
|
|
||||||
* 7 Days to Die (7d2d) [[Separate Query Port](#separate-query-port)]
|
* 7 Days to Die (7d2d) [[Separate Query Port](#separate-query-port)]
|
||||||
|
|
@ -305,7 +312,7 @@ Games List
|
||||||
|
|
||||||
<!--- END GENERATED GAMES -->
|
<!--- END GENERATED GAMES -->
|
||||||
|
|
||||||
###Not supported (yet)
|
### Not supported (yet)
|
||||||
|
|
||||||
* rFactor Engine (rfactor):
|
* rFactor Engine (rfactor):
|
||||||
* rFactor
|
* rFactor
|
||||||
|
|
|
||||||
|
|
@ -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}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
||||||
97
lib/index.js
97
lib/index.js
|
|
@ -29,50 +29,67 @@ 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;
|
||||||
}
|
}
|
||||||
query.debug = Gamedig.debug;
|
query.debug = Gamedig.debug;
|
||||||
query.udpSocket = udpSocket;
|
query.udpSocket = udpSocket;
|
||||||
query.type = options.type;
|
query.type = options.type;
|
||||||
|
|
||||||
if(!('port' in query.options) && ('port_query' in query.options)) {
|
if(!('port' in query.options) && ('port_query' in query.options)) {
|
||||||
if(Gamedig.isCommandLine) {
|
if(Gamedig.isCommandLine) {
|
||||||
process.stderr.write(
|
process.stderr.write(
|
||||||
"Warning! This game is so old, that we don't know"
|
"Warning! This game is so old, that we don't know"
|
||||||
+" what the server's connection port is. We've guessed that"
|
+" what the server's connection port is. We've guessed that"
|
||||||
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
||||||
+" If you know the connection port for this type of server, please let"
|
+" If you know the connection port for this type of server, please let"
|
||||||
+" us know on the GameDig issue tracker, thanks!\n"
|
+" 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);
|
return promise;
|
||||||
|
|
||||||
query.on('finished',function(state) {
|
|
||||||
var i = activeQueries.indexOf(query);
|
|
||||||
if(i >= 0) activeQueries.splice(i, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.nextTick(function() {
|
|
||||||
query.start();
|
|
||||||
});
|
|
||||||
|
|
||||||
return query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
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