mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2025-05-20 02:00:25 -04:00
documentation/examples for server
This commit is contained in:
parent
62e66be053
commit
e783b8a1b3
3 changed files with 146 additions and 0 deletions
33
README.md
33
README.md
|
@ -45,6 +45,39 @@ client.on(0x03, function(packet) {
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Hello World server example
|
||||||
|
|
||||||
|
```js
|
||||||
|
var mc = require('minecraft-protocol');
|
||||||
|
var server = mc.createServer({
|
||||||
|
'online-mode': true, // optional
|
||||||
|
'encryption': true, // optional
|
||||||
|
});
|
||||||
|
server.on('connection', function(client) {
|
||||||
|
client.write(0x01, {
|
||||||
|
entityId: 0,
|
||||||
|
levelType: 'default',
|
||||||
|
gameMode: 0,
|
||||||
|
dimension: 0,
|
||||||
|
difficulty: 2,
|
||||||
|
maxPlayers: 32
|
||||||
|
});
|
||||||
|
client.write(0x0d, {
|
||||||
|
x: 0,
|
||||||
|
y: 1.62,
|
||||||
|
stance: 0,
|
||||||
|
z: 0,
|
||||||
|
yaw: 0,
|
||||||
|
pitch: 0,
|
||||||
|
onGround: true
|
||||||
|
});
|
||||||
|
client.write(0x03, { message: 'Hello, world!' });
|
||||||
|
});
|
||||||
|
server.listen();
|
||||||
|
console.log('Server listening on port', server.port);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
* Ensure your system has the `java` executable in `PATH`.
|
* Ensure your system has the `java` executable in `PATH`.
|
||||||
|
|
76
examples/server.js
Normal file
76
examples/server.js
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
var mc = require('../');
|
||||||
|
|
||||||
|
var yellow = '§e';
|
||||||
|
var players = [];
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
requireAuth: false,
|
||||||
|
motd: 'Vox Industries',
|
||||||
|
maxPlayers: 128,
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = mc.createServer(options);
|
||||||
|
|
||||||
|
server.on('connection', function(client) {
|
||||||
|
var player = {
|
||||||
|
client: client,
|
||||||
|
username: handshake.username,
|
||||||
|
index: players.length
|
||||||
|
};
|
||||||
|
players.push(player);
|
||||||
|
server.players = players.length;
|
||||||
|
|
||||||
|
broadcast(yellow + player.username+' joined the game.');
|
||||||
|
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
|
||||||
|
console.log(player.username+' connected', '('+addr+')');
|
||||||
|
|
||||||
|
client.on('end', function() {
|
||||||
|
players.splice(player.index, 1);
|
||||||
|
server.players = players.length;
|
||||||
|
|
||||||
|
broadcast(yellow + player.username+' left the game.', player);
|
||||||
|
console.log(player.username+' disconnected', '('+addr+')');
|
||||||
|
});
|
||||||
|
|
||||||
|
// send init data so client will start rendering world
|
||||||
|
client.write(0x01, {
|
||||||
|
entityId: 0,
|
||||||
|
levelType: 'default',
|
||||||
|
gameMode: 1,
|
||||||
|
dimension: 0,
|
||||||
|
difficulty: 2,
|
||||||
|
maxPlayers: 128
|
||||||
|
});
|
||||||
|
client.write(0x0d, {
|
||||||
|
x: 0,
|
||||||
|
y: 256,
|
||||||
|
stance: 255,
|
||||||
|
z: 0,
|
||||||
|
yaw: 0,
|
||||||
|
pitch: 0,
|
||||||
|
onGround: 1
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on(0x03, function(data) {
|
||||||
|
var message = '<'+player.username+'>' + ' ' + data.message;
|
||||||
|
broadcast(message);
|
||||||
|
console.log(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
server.on('error', function(error) {
|
||||||
|
console.log('Error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(function() {
|
||||||
|
console.log('Server listening on port', server.port);
|
||||||
|
});
|
||||||
|
|
||||||
|
function broadcast(message, exclude) {
|
||||||
|
for(var i = 0; i < players.length; i++) {
|
||||||
|
if(players[i].username !== exclude && i !== exclude && players[i] !== exclude) {
|
||||||
|
players[i].client.write(0x03, { message: message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
examples/server_helloworld.js
Normal file
37
examples/server_helloworld.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
var mc = require('../');
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
'online-mode': false,
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = mc.createServer(options);
|
||||||
|
|
||||||
|
server.on('connection', function(client) {
|
||||||
|
var addr = client.socket.remoteAddress;
|
||||||
|
console.log('Incoming connection', '('+addr+')');
|
||||||
|
|
||||||
|
client.on('end', function() {
|
||||||
|
console.log('Connection closed', '('+addr+')');
|
||||||
|
});
|
||||||
|
|
||||||
|
// send init data so client will start rendering world
|
||||||
|
client.write(0x0d, {
|
||||||
|
x: 0,
|
||||||
|
y: 1.62,
|
||||||
|
stance: 0,
|
||||||
|
z: 0,
|
||||||
|
yaw: 0,
|
||||||
|
pitch: 0,
|
||||||
|
onGround: true
|
||||||
|
});
|
||||||
|
|
||||||
|
client.write(0x03, { message: 'Hello, world!' });
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('error', function(error) {
|
||||||
|
console.log('Error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(function() {
|
||||||
|
console.log('Server listening on port', server.port);
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue