mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2025-05-19 09:40:25 -04:00
add connect option to createClient for multiple (ping + normal) connections for the proxy examples, use the normal options for pinging in autoVersion to have that option available and fix the proxy examples accordingly
This commit is contained in:
parent
6279ae9afe
commit
010362ef78
6 changed files with 82 additions and 75 deletions
doc
examples
src/client
|
@ -21,6 +21,9 @@ automatically logged in and validated against mojang's auth.
|
||||||
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
|
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
|
||||||
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.
|
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.
|
||||||
The default kicks the client.
|
The default kicks the client.
|
||||||
|
* stream : a stream to use as connection
|
||||||
|
* connect : a function taking the client as parameter and that should client.setSocket(socket)
|
||||||
|
and client.emit('connect') when appropriate (see the proxy examples for an example of use)
|
||||||
|
|
||||||
## mc.Server(version,[customPackets])
|
## mc.Server(version,[customPackets])
|
||||||
|
|
||||||
|
|
|
@ -9,39 +9,41 @@ if(process.argv.length < 6 || process.argv.length > 8) {
|
||||||
const proxyHost=process.argv[4];
|
const proxyHost=process.argv[4];
|
||||||
const proxyPort=process.argv[5];
|
const proxyPort=process.argv[5];
|
||||||
|
|
||||||
const req = Http.request({
|
const client = mc.createClient({
|
||||||
host: proxyHost,
|
connect:(client) => {
|
||||||
port: proxyPort,
|
const req = Http.request({
|
||||||
method: 'CONNECT',
|
host: proxyHost,
|
||||||
path: process.argv[2] + ":" + parseInt(process.argv[3])
|
port: proxyPort,
|
||||||
});
|
method: 'CONNECT',
|
||||||
req.end();
|
path: process.argv[2] + ":" + parseInt(process.argv[3])
|
||||||
|
});
|
||||||
|
req.end();
|
||||||
|
|
||||||
req.on("connect", function(res, stream) {
|
req.on("connect", function(res, stream) {
|
||||||
const client = mc.createClient({
|
client.setSocket(stream);
|
||||||
stream: stream,
|
client.emit('connect');
|
||||||
|
});
|
||||||
|
},
|
||||||
username: process.argv[6] ? process.argv[6] : "echo",
|
username: process.argv[6] ? process.argv[6] : "echo",
|
||||||
password: process.argv[7]
|
password: process.argv[7]
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('connect', function() {
|
client.on('connect', function() {
|
||||||
console.info('connected');
|
console.info('connected');
|
||||||
});
|
});
|
||||||
client.on('disconnect', function(packet) {
|
client.on('disconnect', function(packet) {
|
||||||
console.log('disconnected: '+ packet.reason);
|
console.log('disconnected: '+ packet.reason);
|
||||||
});
|
});
|
||||||
client.on('end', function(err) {
|
client.on('end', function(err) {
|
||||||
console.log('Connection lost');
|
console.log('Connection lost');
|
||||||
});
|
});
|
||||||
client.on('chat', function(packet) {
|
client.on('chat', function(packet) {
|
||||||
const jsonMsg = JSON.parse(packet.message);
|
const jsonMsg = JSON.parse(packet.message);
|
||||||
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
||||||
const username = jsonMsg.with[0].text;
|
const username = jsonMsg.with[0].text;
|
||||||
const msg = jsonMsg.with[1];
|
const msg = jsonMsg.with[1];
|
||||||
if(username === client.username) return;
|
if(username === client.username) return;
|
||||||
client.write('chat', {message: msg});
|
client.write('chat', {message: msg});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
"name": "node-minecraft-protocol-example",
|
"name": "node-minecraft-protocol-example",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {},
|
||||||
},
|
|
||||||
"description": "A node-minecraft-protocol example"
|
"description": "A node-minecraft-protocol example"
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,44 +9,47 @@ if(process.argv.length < 6 || process.argv.length > 8) {
|
||||||
const proxyHost=process.argv[4];
|
const proxyHost=process.argv[4];
|
||||||
const proxyPort=process.argv[5];
|
const proxyPort=process.argv[5];
|
||||||
|
|
||||||
socks.createConnection({
|
const client = mc.createClient({
|
||||||
proxy: {
|
connect: client => {
|
||||||
ipaddress: proxyHost,
|
socks.createConnection({
|
||||||
port: proxyPort,
|
proxy: {
|
||||||
type: 5
|
ipaddress: proxyHost,
|
||||||
},
|
port: proxyPort,
|
||||||
target: {
|
type: 5
|
||||||
host: process.argv[2],
|
},
|
||||||
port: parseInt(process.argv[3])
|
target: {
|
||||||
},
|
host: process.argv[2],
|
||||||
}, function(err, socket) {
|
port: parseInt(process.argv[3])
|
||||||
if (err) {
|
},
|
||||||
console.log(err);
|
}, function(err, socket) {
|
||||||
return;
|
if (err) {
|
||||||
}
|
console.log(err);
|
||||||
const client = mc.createClient({
|
return;
|
||||||
stream: socket,
|
}
|
||||||
username: process.argv[6] ? process.argv[6] : "echo",
|
|
||||||
password: process.argv[7]
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('connect', function() {
|
client.setSocket(socket);
|
||||||
console.info('connected');
|
client.emit('connect');
|
||||||
});
|
});
|
||||||
client.on('disconnect', function(packet) {
|
},
|
||||||
console.log('disconnected: '+ packet.reason);
|
username: process.argv[6] ? process.argv[6] : "echo",
|
||||||
});
|
password: process.argv[7]
|
||||||
client.on('end', function(err) {
|
|
||||||
console.log('Connection lost');
|
|
||||||
});
|
|
||||||
client.on('chat', function(packet) {
|
|
||||||
const jsonMsg = JSON.parse(packet.message);
|
|
||||||
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
|
||||||
const username = jsonMsg.with[0].text;
|
|
||||||
const msg = jsonMsg.with[1];
|
|
||||||
if(username === client.username) return;
|
|
||||||
client.write('chat', {message: msg});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on('connect', function() {
|
||||||
|
console.info('connected');
|
||||||
|
});
|
||||||
|
client.on('disconnect', function(packet) {
|
||||||
|
console.log('disconnected: '+ packet.reason);
|
||||||
|
});
|
||||||
|
client.on('end', function(err) {
|
||||||
|
console.log('Connection lost');
|
||||||
|
});
|
||||||
|
client.on('chat', function(packet) {
|
||||||
|
const jsonMsg = JSON.parse(packet.message);
|
||||||
|
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
||||||
|
const username = jsonMsg.with[0].text;
|
||||||
|
const msg = jsonMsg.with[1];
|
||||||
|
if(username === client.username) return;
|
||||||
|
client.write('chat', {message: msg});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -9,9 +9,8 @@ const minecraft_data = require('minecraft-data');
|
||||||
module.exports = function(client, options) {
|
module.exports = function(client, options) {
|
||||||
client.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
|
client.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
|
||||||
debug('pinging',options.host);
|
debug('pinging',options.host);
|
||||||
const pingOptions = {host: options.host, port: options.port};
|
|
||||||
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
|
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
|
||||||
ping(pingOptions, function(err, response) {
|
ping(options, function(err, response) {
|
||||||
if (err) throw err; // hmm
|
if (err) throw err; // hmm
|
||||||
debug('ping response',response);
|
debug('ping response',response);
|
||||||
// TODO: could also use ping pre-connect to save description, type, max players, etc.
|
// TODO: could also use ping pre-connect to save description, type, max players, etc.
|
||||||
|
|
|
@ -5,7 +5,8 @@ module.exports = function(client, options) {
|
||||||
options.port = options.port || 25565;
|
options.port = options.port || 25565;
|
||||||
options.host = options.host || 'localhost';
|
options.host = options.host || 'localhost';
|
||||||
|
|
||||||
options.connect = (client) => {
|
if(!options.connect)
|
||||||
|
options.connect = (client) => {
|
||||||
if (options.stream) {
|
if (options.stream) {
|
||||||
client.setSocket(options.stream);
|
client.setSocket(options.stream);
|
||||||
client.emit('connect');
|
client.emit('connect');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue