Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mcpews/mcpews
- Owner: mcpews
- License: mit
- Created: 2021-09-10T08:32:58.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-10-25T13:00:26.000Z (about 1 year ago)
- Last Synced: 2024-04-26T05:44:52.725Z (8 months ago)
- Language: TypeScript
- Size: 346 KB
- Stars: 15
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-minecraft-bedrock - mcpews - A library that supports MCPE Websocket Protocol (Networking / Protocol)
README
# MCPEWS
A library that supports MCPE Websocket Protocol.
## Usage
Server-side:
```javascript
import { WSServer, Version } from 'mcpews';
const server = new WSServer(19134); // portserver.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'); // addressprocess.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 []
```