Last active
December 11, 2015 19:49
-
-
Save roblabla/4651249 to your computer and use it in GitHub Desktop.
node-minecraft-protocol manual testing ping.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mc = require('../'); | |
var client = mc.ping({}, function (err,results) { | |
if (err !== null) { | |
console.log(err); | |
} else { | |
console.log(results); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mc = require('../'); | |
var yellow = '§e'; | |
var options = { | |
motd: 'LETZ DO ZIS', | |
'max-players': 122, | |
port: 25565, | |
}; | |
var server = mc.createServer(options); | |
server.on('login', function(client) { | |
broadcast(yellow + client.username+' joined the game.'); | |
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort; | |
console.log(client.username+' connected', '('+addr+')'); | |
client.on('end', function() { | |
broadcast(yellow + client.username+' left the game.', client); | |
console.log(client.username+' disconnected', '('+addr+')'); | |
}); | |
// send init data so client will start rendering world | |
client.write(0x01, { | |
entityId: client.id, | |
levelType: 'default', | |
gameMode: 1, | |
dimension: 0, | |
difficulty: 2, | |
maxPlayers: server.maxPlayers | |
}); | |
client.write(0x0d, { | |
x: 0, | |
y: 256, | |
stance: 255, | |
z: 0, | |
yaw: 0, | |
pitch: 0, | |
onGround: true | |
}); | |
client.on(0x03, function(data) { | |
var message = '<'+client.username+'>' + ' ' + data.message; | |
broadcast(message); | |
console.log(message); | |
}); | |
}); | |
server.on('error', function(error) { | |
console.log('Error:', error); | |
}); | |
server.on('listening', function() { | |
console.log('Server listening on port', server.socketServer.address().port); | |
}); | |
function broadcast(message, exclude) { | |
var client; | |
for (var clientId in server.clients) { | |
client = server.clients[clientId]; | |
if (client !== exclude) client.write(0x03, { message: message }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("pings the server", function(done) { | |
startServer({ | |
motd: 'test1234', | |
'max-players': 120, | |
}, function() { | |
mc.ping({}, function(err, results) { | |
console.log(results); | |
if (err) return done(err); | |
assert.deepEqual(results, { | |
prefix: "§1", | |
protocol: protocol.version, | |
version: protocol.minecraftVersion, | |
motd: 'test1234', | |
playerCount: 0, | |
maxPlayers: 120 | |
}); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment