Add ipc connection option for servers (#1113)

* Add ipc connection option for servers

* Fix linting error
This commit is contained in:
IceTank 2023-05-20 15:03:55 +02:00 committed by GitHub
parent d6b18b1dad
commit d9e37e3d76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 3 deletions

View file

@ -33,6 +33,7 @@ automatically logged in and validated against mojang's auth.
* validateChannelProtocol (optional) : whether or not to enable protocol validation for custom protocols using plugin channels for the connected clients. Defaults to true
* enforceSecureProfile (optional) : Kick clients that do not have chat signing keys from Mojang (1.19+)
* generatePreview (optional) : Function to generate chat previews. Takes the raw message string and should return the message preview as a string. (1.19-1.19.2)
* socketType (optional) : either `tcp` or `ipc`. Switches from a tcp connection to a ipc socket connection (or named pipes on windows). With the `ipc` option `host` becomes the path off the ipc connection on the local filesystem. Example: `\\.\pipe\minecraft-ipc` (Windows) `/tmp/minecraft-ipc.sock` (unix based systems). See the ipcConnection example for an example.
## mc.Server(version,[customPackets])

View file

@ -0,0 +1,51 @@
/** IPC Connection example
*
* This example shows how to use a IPC connection to communicate with a server or client.
*
* See the node.js documentation about IPC connections here: https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections
*/
const nmp = require('minecraft-protocol')
const net = require('net')
const ipcName = 'minecraft-ipc'
// IPC with node.js works differently on windows and unix systems
let ipcPath
if (process.platform === 'win32') {
ipcPath = `\\\\.\\pipe\\${ipcName}`
} else {
ipcPath = `/tmp/${ipcName}.sock`
}
const server = nmp.createServer({
version: '1.18.2',
socketType: 'ipc',
host: ipcPath, // When the optional option socketType is 'ipc' the host becomes the socket path
'online-mode': false
})
server.on('listening', () => {
console.info('Server listening on', server.socketServer.address())
connectAClient()
})
server.on('login', (client) => {
console.info(`New user '${client.username}' logged into the server`)
})
function connectAClient () {
const client = nmp.createClient({
version: '1.18.2',
username: 'ipc_client',
connect: (client) => {
const socket = net.connect(ipcPath, () => {
client.setSocket(socket)
client.emit('connect')
})
},
auth: 'offline'
})
client.on('connect', () => console.info('Client connected to server'))
client.on('end', () => console.info('Client disconnected from server'))
}

View file

@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}

View file

@ -23,7 +23,8 @@ function createServer (options = {}) {
version,
favicon,
customPackets,
motdMsg // This is when you want to send formated motd's from ChatMessage instances
motdMsg, // This is when you want to send formated motd's from ChatMessage instances
socketType = 'tcp'
} = options
const maxPlayers = options['max-players'] !== undefined ? maxPlayersOld : maxPlayersNew
@ -63,6 +64,10 @@ function createServer (options = {}) {
server.on('connection', function (client) {
plugins.forEach(plugin => plugin(client, server, options))
})
if (socketType === 'ipc') {
server.listen(host)
} else {
server.listen(port, host)
}
return server
}

1
src/index.d.ts vendored
View file

@ -192,6 +192,7 @@ declare module 'minecraft-protocol' {
enforceSecureProfile?: boolean
// 1.19.1 & 1.19.2 only: If client should send previews of messages they are typing to the server
enableChatPreview?: boolean
socketType?: 'tcp' | 'ipc'
}
export interface SerializerOptions {