Added gameserver latency to returning data

This commit is contained in:
Kristjan Kirpu 2015-08-03 17:05:30 +03:00
parent ae8c13ba69
commit 618e19192c
2 changed files with 13 additions and 2 deletions

View file

@ -34,7 +34,8 @@
"request": "~2.51.0", "request": "~2.51.0",
"minimist": "~1.1.0", "minimist": "~1.1.0",
"varint": "~4.0.0", "varint": "~4.0.0",
"moment": "~2.9.0" "moment": "~2.9.0",
"jjg-ping": "~1.0.1"
}, },
"bin": { "bin": {
"gamedig": "bin/gamedig.js" "gamedig": "bin/gamedig.js"

View file

@ -3,7 +3,8 @@ var EventEmitter = require('events').EventEmitter,
net = require('net'), net = require('net'),
async = require('async'), async = require('async'),
Class = require('../lib/Class'), Class = require('../lib/Class'),
Reader = require('../lib/reader'); Reader = require('../lib/reader'),
ping = require('jjg-ping');
module.exports = Class.extend(EventEmitter,{ module.exports = Class.extend(EventEmitter,{
init: function() { init: function() {
@ -18,6 +19,7 @@ module.exports = Class.extend(EventEmitter,{
this.encoding = 'utf8'; this.encoding = 'utf8';
this.byteorder = 'le'; this.byteorder = 'le';
this.delimiter = '\0'; this.delimiter = '\0';
this.ping = 0;
var self = this; var self = this;
this.globalTimeoutTimer = setTimeout(function() { this.globalTimeoutTimer = setTimeout(function() {
@ -68,6 +70,7 @@ module.exports = Class.extend(EventEmitter,{
if('port_query' in this.options) state.query.port_query = this.options.port_query; if('port_query' in this.options) state.query.port_query = this.options.port_query;
state.query.type = this.type; state.query.type = this.type;
if('pretty' in this) state.query.pretty = this.pretty; if('pretty' in this) state.query.pretty = this.pretty;
state.query.server_ping = this.ping;
this.reset(); this.reset();
this.finished = true; this.finished = true;
@ -107,6 +110,13 @@ module.exports = Class.extend(EventEmitter,{
self.parseDns(options.host,c); self.parseDns(options.host,c);
} }
}, },
function(c) {
// not sure if this is the best way to ping a host
ping.system.ping(options.host, function(latency, status) {
if(status) self.ping = latency;
});
c();
},
function(c) { function(c) {
// calculate query port if needed // calculate query port if needed
if(!('port_query' in options) && 'port' in options) { if(!('port_query' in options) && 'port' in options) {