node-minecraft-protocol/src/ping.js
Romain Beaumont f45c6dff49 use protodef:
* move general datatypes to protodef along with their tests
* move states to states.js file
* use one protodef serializer by state and direction instead of one big serializer for everything (same thing for the deserializer)
* define a packet as a protodef type using a switch and a container, and adding each minecraft packet as a type (packet_ + name)
* use mapper type from protodef to convert id to name in packet definition
* use general string type : pstring
* divide by 10 the number of iteration in the benchmark to get back to a reasonable test execution time
2015-11-08 23:29:51 +01:00

46 lines
1.2 KiB
JavaScript

var net = require('net');
var Client = require('./client');
var states = require("./states");
module.exports = ping;
function ping(options, cb) {
var host = options.host || 'localhost';
var port = options.port || 25565;
var optVersion = options.version || require("./version").defaultVersion;
var mcData=require("minecraft-data")(optVersion);
var version = mcData.version;
var client = new Client(false,version.majorVersion);
client.on('error', function(err) {
cb(err);
});
client.once('server_info', function(packet) {
var data = JSON.parse(packet.response);
var start = Date.now();
client.once('ping', function(packet) {
data.latency = Date.now() - start;
cb(null, data);
client.end();
});
client.write('ping', {time: [0, 0]});
});
client.on('state', function(newState) {
if(newState === states.STATUS)
client.write('ping_start', {});
});
client.on('connect', function() {
client.write('set_protocol', {
protocolVersion: version.version,
serverHost: host,
serverPort: port,
nextState: 1
});
client.state = states.STATUS;
});
client.connect(port, host);
}