mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2025-02-15 23:39:45 -05:00
add electron example, fix #751
This commit is contained in:
parent
4d092bb4dc
commit
50a90030b3
6 changed files with 159 additions and 0 deletions
4
examples/client_electron/README.md
Normal file
4
examples/client_electron/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# client_electron
|
||||
|
||||
* npm install
|
||||
* npm start
|
33
examples/client_electron/Window.js
Normal file
33
examples/client_electron/Window.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
'use strict'
|
||||
|
||||
const { BrowserWindow } = require('electron')
|
||||
|
||||
// default window settings
|
||||
const defaultProps = {
|
||||
width: 500,
|
||||
height: 800,
|
||||
show: false,
|
||||
|
||||
// update for electron V5+
|
||||
webPreferences: {
|
||||
nodeIntegration: true
|
||||
}
|
||||
}
|
||||
|
||||
class Window extends BrowserWindow {
|
||||
constructor ({ file, ...windowSettings }) {
|
||||
// calls new BrowserWindow with these props
|
||||
super({ ...defaultProps, ...windowSettings })
|
||||
|
||||
// load the html and open devtools
|
||||
this.loadFile(file)
|
||||
// this.webContents.openDevTools()
|
||||
|
||||
// gracefully show when ready to prevent flickering
|
||||
this.once('ready-to-show', () => {
|
||||
this.show()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Window
|
43
examples/client_electron/main.js
Normal file
43
examples/client_electron/main.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const { app, ipcMain } = require('electron')
|
||||
const mc = require('minecraft-protocol')
|
||||
|
||||
const Window = require('./Window')
|
||||
|
||||
require('electron-reload')(__dirname)
|
||||
|
||||
function main () {
|
||||
const mainWindow = new Window({
|
||||
file: path.join('renderer', 'index.html')
|
||||
})
|
||||
|
||||
mainWindow.once('show', () => {
|
||||
})
|
||||
|
||||
ipcMain.on('connect', (e, data) => {
|
||||
const client = mc.createClient(data)
|
||||
client.on('login', () => mainWindow.send('content', 'connected'))
|
||||
let chat = ''
|
||||
|
||||
client.on('chat', function (packet) {
|
||||
const jsonMsg = JSON.parse(packet.message)
|
||||
if (jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
||||
const username = jsonMsg.with[0].text
|
||||
const msg = jsonMsg.with[1]
|
||||
chat += `${username} > ${msg}<br />`
|
||||
mainWindow.send('content', chat)
|
||||
}
|
||||
})
|
||||
ipcMain.on('chat', (e, chat2) => {
|
||||
client.write('chat', { message: chat2 })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
app.on('ready', main)
|
||||
|
||||
app.on('window-all-closed', function () {
|
||||
app.quit()
|
||||
})
|
17
examples/client_electron/package.json
Normal file
17
examples/client_electron/package.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "node-minecraft-protocol-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"description": "A node-minecraft-protocol example",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"start": "electron ."
|
||||
},
|
||||
"devDependencies": {
|
||||
"devtron": "^1.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"electron": "^3.0.4",
|
||||
"electron-reload": "^1.2.5"
|
||||
}
|
||||
}
|
24
examples/client_electron/renderer/index.html
Normal file
24
examples/client_electron/renderer/index.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>client electron</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
Host: <input type="text" id="host" value="localhost" /><br />
|
||||
Port: <input type="text" id="port" value="25565"/><br />
|
||||
Username: <input type="text" id="username" value="electron_client" /><br />
|
||||
Password: <input type="text" id="password" value="" /><br />
|
||||
|
||||
<button id="connect" type="button">Connect</button><br />
|
||||
<div id="content">Not connected</div> <br />
|
||||
Message: <input type="text" id="chat" /><br />
|
||||
<button id="send" type="button">Send</button><br />
|
||||
|
||||
<script src="./index.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
38
examples/client_electron/renderer/index.js
Normal file
38
examples/client_electron/renderer/index.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
'use strict'
|
||||
|
||||
const { ipcRenderer } = require('electron')
|
||||
|
||||
function setContent (content) {
|
||||
const contentItem = document.getElementById('content')
|
||||
|
||||
contentItem.innerHTML = content
|
||||
}
|
||||
|
||||
document.getElementById('connect').addEventListener('click', () => {
|
||||
setContent('connecting...')
|
||||
const data = {
|
||||
host: document.getElementById('host').value,
|
||||
port: parseInt(document.getElementById('port').value),
|
||||
username: document.getElementById('username').value,
|
||||
password: document.getElementById('password').value === '' ? undefined : document.getElementById('password').value
|
||||
}
|
||||
ipcRenderer.send('connect', data)
|
||||
})
|
||||
|
||||
function chat () {
|
||||
ipcRenderer.send('chat', document.getElementById('chat').value)
|
||||
document.getElementById('chat').value = ''
|
||||
}
|
||||
document.getElementById('chat').addEventListener('keyup', function onEvent (e) {
|
||||
if (e.keyCode === 13) {
|
||||
chat()
|
||||
}
|
||||
})
|
||||
|
||||
document.getElementById('send').addEventListener('click', () => {
|
||||
chat()
|
||||
})
|
||||
|
||||
ipcRenderer.on('content', (event, content) => {
|
||||
setContent(content)
|
||||
})
|
Loading…
Reference in a new issue