diff --git a/examples/client_forge/client_forge.js b/examples/client_forge/client_forge.js index 0f0b079..cd8d9ed 100644 --- a/examples/client_forge/client_forge.js +++ b/examples/client_forge/client_forge.js @@ -7,38 +7,10 @@ if(process.argv.length < 4 || process.argv.length > 6) { process.exit(1); } -var client = mc.createClient({ - forge: true, - // Client/server mods installed on the client - // TODO: send from ServerListPing packet - forgeMods: - [ - {modid:'IronChest', version:'6.0.121.768'} - ], - host: process.argv[2], - port: parseInt(process.argv[3]), - username: process.argv[4] ? process.argv[4] : "echo", - password: process.argv[5] -}); - -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) { - var jsonMsg = JSON.parse(packet.message); - if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') { - var username = jsonMsg.with[0].text; - var msg = jsonMsg.with[1]; - if(username === client.username) return; - client.write('chat', {message: msg}); - } -}); +var host = process.argv[2]; +var port = parseInt(process.argv[3]); +var username = process.argv[4] ? process.argv[4] : "echo"; +var password = process.argv[5]; var proto = new ProtoDef(); // copied from ../../dist/transforms/serializer.js TODO: refactor @@ -299,16 +271,66 @@ function fmlHandshakeStep(client, data) } } -client.on('custom_payload', function(packet) { - var channel = packet.channel; - var data = packet.data; +//var forgeMods; // = [ {modid:'IronChest', version:'6.0.121.768'} ]; - if (channel === 'REGISTER') { - var channels = data.toString().split('\0'); - console.log('Server-side registered channels:',channels); - // TODO: do something? - // expect: [ 'FML|HS', 'FML', 'FML|MP', 'FML', 'FORGE' ] - } else if (channel === 'FML|HS') { - fmlHandshakeStep(client, data); +mc.ping({host, port}, function(err, response) { + if (err) throw err; + console.log('ping response',response); + if (!response.modinfo || response.modinfo.type !== 'FML') { + throw new Error('not an FML server, aborting connection'); + // TODO: gracefully connect non-FML + // TODO: could also use ping pre-connect to save description, type, negotiate protocol etc. } + // Use the list of Forge mods from the server ping, so client will match server + var forgeMods = response.modinfo.modList; + console.log('Using forgeMods:',forgeMods); + + var client = mc.createClient({ + forge: true, + forgeMods: forgeMods, + // Client/server mods installed on the client + // if not specified, pings server and uses its list + /* + forgeMods: + */ + host: host, + port: port, + username: username, + password: password + }); + + 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) { + var jsonMsg = JSON.parse(packet.message); + if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') { + var username = jsonMsg.with[0].text; + var msg = jsonMsg.with[1]; + if(username === client.username) return; + client.write('chat', {message: msg}); + } + }); + + client.on('custom_payload', function(packet) { + var channel = packet.channel; + var data = packet.data; + + if (channel === 'REGISTER') { + var channels = data.toString().split('\0'); + console.log('Server-side registered channels:',channels); + // TODO: do something? + // expect: [ 'FML|HS', 'FML', 'FML|MP', 'FML', 'FORGE' ] + } else if (channel === 'FML|HS') { + fmlHandshakeStep(client, data); + } + }); }); + +