生成一个能与mc服务器交互的网页前端并给予实例
下面是一个简单的示例,用于与Minecraft服务器交互的网页前端。
<!DOCTYPE html>
<html>
<head>
<title>Minecraft 服务器交互</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#status {
font-weight: bold;
}
#output {
margin-top: 10px;
border: 1px solid #ccc;
padding: 10px;
height: 200px;
overflow-y: scroll;
}
#input {
margin-top: 10px;
width: 100%;
padding: 5px;
}
#submit {
margin-top: 10px;
padding: 5px 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Minecraft 服务器交互</h1>
<div id="status">连接状态:未连接</div>
<div id="output"></div>
<input type="text" id="input" placeholder="输入命令">
<button id="submit">发送命令</button>
<script>
var socket = new WebSocket("ws://your-minecraft-server-ip:your-minecraft-server-port");
socket.onopen = function(event) {
document.getElementById("status").innerHTML = "连接状态:已连接";
};
socket.onmessage = function(event) {
var outputElement = document.getElementById("output");
outputElement.innerHTML += event.data + "<br>";
outputElement.scrollTop = outputElement.scrollHeight;
};
socket.onclose = function(event) {
document.getElementById("status").innerHTML = "连接状态:连接关闭";
};
document.getElementById("submit").addEventListener("click", function() {
var inputElement = document.getElementById("input");
var command = inputElement.value;
socket.send(command);
inputElement.value = "";
});
</script>
</body>
</html>
在上面的示例中,你需要将ws://your-minecraft-server-ip:your-minecraft-server-port替换为你的Minecraft服务器的IP地址和端口号。
这个网页前端使用WebSocket与Minecraft服务器建立连接,并通过输入框发送命令到服务器。服务器返回的输出会显示在页面上。页面上还有一个连接状态的显示区域
原文地址: https://www.cveoy.top/t/topic/iBAc 著作权归作者所有。请勿转载和采集!