node-minecraft-protocol/examples/client_echo/client_echo.js

33 lines
951 B
JavaScript
Raw Normal View History

var mc = require('minecraft-protocol');
2015-09-20 16:19:39 -04:00
if(process.argv.length < 4 || process.argv.length > 6) {
console.log("Usage : node echo.js <host> <port> [<name>] [<password>]");
process.exit(1);
}
2013-01-02 03:30:33 -05:00
var client = mc.createClient({
2015-09-20 16:19:39 -04:00
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : "echo",
password: process.argv[5]
2013-01-02 03:30:33 -05:00
});
2015-09-20 16:19:39 -04:00
2013-01-02 03:30:33 -05:00
client.on('connect', function() {
2013-07-09 02:11:09 -04:00
console.info('connected');
2013-01-02 03:30:33 -05:00
});
2016-01-22 03:32:36 -05:00
client.on('disconnect', function(packet) {
console.log('disconnected: '+ packet.reason);
});
client.on('end', function(err) {
console.log('Connection lost');
});
2014-01-14 00:56:56 -05:00
client.on('chat', function(packet) {
2013-07-09 02:11:09 -04:00
var jsonMsg = JSON.parse(packet.message);
if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') {
2015-09-20 16:19:39 -04:00
var username = jsonMsg.with[0].text;
var msg = jsonMsg.with[1];
if(username === client.username) return;
client.write('chat', {message: msg});
2013-07-09 02:11:09 -04:00
}
2015-03-26 11:25:36 -04:00
});