├── README.md ├── index.js ├── lib └── ping.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # mc-ping 2 | 3 | ![license](http://img.shields.io/npm/l/mc-ping.png?style=flat) 4 | ![stable](http://img.shields.io/npm/v/mc-ping.png?style=flat) 5 | [![package](http://img.shields.io/npm/mc-ping.png?style=flat)](https://www.npmjs.org/package/mc-ping) 6 | 7 | # Deprecated 8 | 9 | This package is deprecated in favour of [mc-ping-updated](https://www.npmjs.com/package/mc-ping-updated) ([GitHub](https://github.com/Cryptkeeper/mc-ping-updated)). Please use it instead of this package. 10 | 11 | # Original Readme 12 | 13 | This is a super-simple library that provides access to the [Server-list-ping](http://wiki.vg/Server_List_Ping) feature of Minecraft servers. 14 | 15 | You can use it as follows 16 | 17 | mcping = require('mc-ping'); 18 | mcping('example.com', 25565, function(err, res) { 19 | if (err) { 20 | // Some kind of error 21 | console.error(err); 22 | } else { 23 | // Success! 24 | console.log(res); 25 | } 26 | }, 3000); 27 | 28 | In this instance, `res` will be a javascript object similar to the following: 29 | 30 | { protocol_version: '51', 31 | minecraft_version: '1.4.7', 32 | server_name: 'Your Server MOTD', 33 | num_players: '0', 34 | max_players: '20' } 35 | 36 | ## License 37 | 38 | (MIT License) 39 | 40 | Copyright (C) 2013 David White <david@wizardfrag.co.uk> 41 | 42 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 43 | 44 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 45 | 46 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 47 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * mc-ping 3 | * Copyright (c) 2013 David White 4 | * MIT Licensed 5 | **/ 6 | 7 | module.exports = require('./lib/ping.js'); -------------------------------------------------------------------------------- /lib/ping.js: -------------------------------------------------------------------------------- 1 | /** 2 | * mc-ping 3 | * Copyright (c) 2013 David White 4 | * MIT Licensed 5 | **/ 6 | 7 | var net = require("net"); 8 | 9 | var ping = function(server, port, callback, timeout) { 10 | var MC_DEFAULT_PORT = 25565; 11 | if (typeof port == "function") { 12 | callback = port; 13 | port = MC_DEFAULT_PORT; 14 | } 15 | 16 | if (typeof port !== "number") { 17 | port = MC_DEFAULT_PORT; 18 | } 19 | 20 | if (typeof timeout == "undefined") { 21 | timeout = 3000; 22 | } 23 | 24 | var socket = net.connect({ 25 | port: port, 26 | host: server 27 | }, function() { 28 | buf = new Buffer(2); 29 | buf[0] = 254; 30 | buf[1] = 1; 31 | socket.write(buf); 32 | }); 33 | 34 | socket.setTimeout(timeout, function () { 35 | if (callback != undefined) { 36 | callback(new Error("Socket timed out when connecting to " + server + ":" + port), null); 37 | } 38 | socket.end(); 39 | }); 40 | 41 | socket.on("data", function(data) { 42 | var newdata = []; 43 | if (data[0] == 255) { // Server's magic response 44 | var iszero = false, 45 | y = 0; 46 | for (var i = 1; i < data.length; i++) { 47 | if (data[i] === 0 && iszero) { // Separator 48 | if (newdata[y].length > 0) 49 | y++; 50 | newdata[y] = ""; 51 | continue; 52 | } 53 | if (newdata[y] === undefined) { 54 | newdata[y] = ""; 55 | } 56 | if (data[i] !== 0) { 57 | iszero = false; 58 | var newchar = String.fromCharCode(data[i]); 59 | if (newchar !== undefined) { 60 | newdata[y] = newdata[y] + newchar; 61 | } 62 | } else { 63 | iszero = true; 64 | } 65 | } 66 | if (callback !== undefined) { 67 | data = {}; 68 | data.protocol_version = newdata[1]; 69 | data.minecraft_version = newdata[2]; 70 | data.server_name = newdata[3]; 71 | data.num_players = newdata[4]; 72 | data.max_players = newdata[5]; 73 | callback(null, data); 74 | } 75 | } else { 76 | callback("Unexpected data from server", null); 77 | } 78 | socket.end(); 79 | }); 80 | 81 | socket.once('error', function(e) { 82 | if (callback !== undefined) { 83 | callback(e, null); 84 | } 85 | }); 86 | }; 87 | 88 | module.exports = ping; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mc-ping", 3 | "version": "0.0.3", 4 | "description": "Pings a Minecraft server for its MOTD and other data", 5 | "main": "index", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/wizardfrag/mc-ping.git" 9 | }, 10 | "keywords": [ 11 | "minecraft", 12 | "ping", 13 | "utilities" 14 | ], 15 | "author": "David White ", 16 | "license": "MIT" 17 | } 18 | --------------------------------------------------------------------------------