Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mcpews/mcpews

A library that supports MCPE Websocket Protocol
https://github.com/mcpews/mcpews

Last synced: 24 days ago
JSON representation

A library that supports MCPE Websocket Protocol

Awesome Lists containing this project

README

        

# MCPEWS

A library that supports MCPE Websocket Protocol.

## Usage

Server-side:
```javascript
import { WSServer, Version } from 'mcpews';
const server = new WSServer(19134); // port

server.on('client', ({ session }) => {
// someone type "/connect :19134" in the game console

// execute a command
session.sendCommand('say Connected!');

// execute a command and receive the response
session.sendCommand('list', ({ body }) => {
console.log(`currentPlayerCount = ${body.currentPlayerCount}`);
});

// subscribe a event
session.subscribe('PlayerMessage', (event) => {
// when event triggered
const { body, version } = event;
let message, messageType;
if (version === Version.V1_1_0) {
message = body.message;
messageType = body.type;
} else {
message = body.properties.Message;
messageType = body.properties.MessageType;
}
if (message === 'close') {
// disconnect from the game
session.disconnect();
} else if (messageType === 'chat') {
session.sendCommand('say You just said ' + message);
}
});

// enable encrypted connection
session.enableEncryption();
});
```

Client-side:
```javascript
import { WSClient } from "mcpews";
const client = new WSClient('ws://127.0.0.1:19134'); // address

process.stdin.on('data', (buffer) => {
// trigger a event (will be ignored if not subscribed)
client.publishEvent('input', {
data: buffer.toString()
});
});

client.on('command', (event) => {
const { commandLine } = event;

// pass encryption handshake to client itself
if (event.handleEncryptionHandshake()) return;

// command received
console.log('command: ' + commandLine);

// respond the command, must be called after handling
event.respond({
length: commandLine.length
});
});
```

WSApp, optimized for async/await:
```javascript
import { WSApp } from "mcpews";

const app = new WSApp(19134);
app.on('session', async ({ session }) => {
const playerNames = (await session.command('testfor @a')).body.victim;
const names = await Promise.all(
playerNames.map(async (playerName) => {
await session.command(`tell ${playerName} What's your name?`);
try {
const name = (await session.waitForEvent('PlayerMessage', 30000, (ev) => ev.body.sender === playerName))
.body.message;
await session.command(`tell ${playerName} Your name is ${name}`);
return name;
} catch (err) {
return playerName;
}
})
);
console.log(names);
await session.disconnect();
});
```

REPL:
```
mcpews []
```

MITM:
```
mcpewsmitm []
```