Skip to content

Commit

Permalink
Merge pull request #3 from Tuisku-L/feature/ServerConsole-WS
Browse files Browse the repository at this point in the history
feat: 实时推送服务器输出
  • Loading branch information
Woo0ood authored Aug 26, 2019
2 parents daa378c + 54f309b commit c51e183
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# (2019-08-26)


### Bug Fixes

* 修复 当配置文件中 EventList 为空或无 EventList 配置项时导致插件启动异常 ([59b3245](https://github.com/Tuisku-L/JSONAPI-NukkitX/commit/59b3245))

### Features


* 添加 ServerConsole 监听, 通过 WebSocket 获取服务器实时输出, 通过 WebSocket 执行命令(开关) ([a68dfad](https://github.com/Tuisku-L/JSONAPI-NukkitX/commit/a68dfad))

# (2019-08-23)


Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
</h1>

[![](https://img.shields.io/badge/NUKKIT-1.0-blue?style=flat-square)](https://ci.nukkitx.com/job/NukkitX/job/Nukkit/job/master/)
![](https://img.shields.io/badge/JDK-≥1.8-blue?style=flat-square)
[![](https://img.shields.io/badge/LICENSE-MIT-green?style=flat-square)](./LICENSE)

# 简介
Expand Down Expand Up @@ -67,6 +68,9 @@ Server:

EventListener:
IsEnable: true
ServerConsole: true
OtherEvents: true
ExecuteByWs: true
EventList:
- ServerCommand
- PlayerChat
Expand All @@ -81,7 +85,10 @@ DebugMode: true
- Server.HttpPort 是 API 服务器监听的端口,**请不要与 Nukkit(X) 本身的端口冲突**。
- Server.WsPort 是 W1ebSocket 服务器监听的端口,**请不要与 Nukkit(X) 本身的端口冲突**。
- EventListener.IsEnable 配置事件通知的 WebSocket 服务是否启用。
- EventListener.EventList 如果 `EventListener.IsEnable` 为 true ,则这个列表下的事件会被 JSONAPI-Nukkit(X) 监听,并且通过 WebSocket 发送实时通知。
- EventListener.ServerConsole 配置是否通过 WebSocket 发送服务器实时输出。
- EventListener.OtherEvents 配置是否监听除了服务器实时输出外的其他 JSONAPI-Nukkit(X) 事件。
- EventListener.ExecuteByWs 配置是否可以通过 WebSocket 执行命令。
- EventListener.EventList 如果 `EventListener.OtherEvents` 为 true ,则这个列表下的事件会被 JSONAPI-Nukkit(X) 监听,并且通过 WebSocket 发送实时通知。
- DebugMode 配置是否启动 Debug 模式,若为 true 则会在控制台输出调试信息并且调用 API 时无需鉴权信息。

# 下载
Expand All @@ -92,6 +99,7 @@ DebugMode: true

### 已知问题
- 在调用 `/api/Server/ExecuteCommand` 以运行一条命令时,控制台将会抛出一个异常,原因是不在主线程执行命令时会抛出错误,但是命令还是会正常执行。等待 Nukkit(X)官方修复此问题。
- 如果开启了 "ExecuteByWs",则每次通过 WebSocket 执行命令时,控制台都会抛出一个异常,原因同上。

# 更新日志
本项目遵从 [Angular Style Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153),更新日志由 `conventional-changelog` 自动生成。完整日志请点击 [CHANGELOG.md](./CHANGELOG.md)。
Expand Down
3 changes: 3 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Server:

EventListener:
IsEnable: true
ServerConsole: true
OtherEvents: true
ExecuteByWs: true
EventList:
- ServerCommand
- PlayerChat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tech.v2c.minecraft.plugins.jsonApi.EventNotify.Console;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import tech.v2c.minecraft.plugins.jsonApi.EventNotify.global.JsonApiWebSocketServer;
import tech.v2c.minecraft.plugins.jsonApi.tools.results.JsonResult;

import java.util.Date;
import java.util.HashMap;

public class ServerConsoleEvent extends AbstractAppender {
public ServerConsoleEvent() {
super("JSONAPI", null, null);
}

@Override
public void append(LogEvent logEvent) {
HashMap<Object, Object> result = new HashMap<Object, Object>();
result.put("type", "ServerConsoleEvent");
result.put("message", "[" + new Date().toString() + "] " + "[" + logEvent.getLevel().name() + "] " + logEvent.getMessage().getFormattedMessage());

JsonApiWebSocketServer.SendMsg(new JsonResult(result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

import cn.nukkit.event.Listener;
import tech.v2c.minecraft.plugins.jsonApi.JsonApi;
import tech.v2c.minecraft.plugins.jsonApi.tools.LogUtils;
import tech.v2c.minecraft.plugins.jsonApi.tools.gameUtils.EventUtils;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;

public class EventManage {
public static HashMap<String, Listener> allEvent = new HashMap<String, Listener> ();
public static HashMap<String, Listener> allEvent = new HashMap<String, Listener>();

public static void RegisterEventListener(){
public static void RegisterEventListener() {
List events = JsonApi.instance.getConfig().getSection("EventListener").getList("EventList");
allEvent.entrySet().forEach(val -> {
if(events.contains(val.getKey())){
EventUtils.RegisterEvent(val.getValue());
}
});
if (events != null) {
allEvent.entrySet().forEach(val -> {
if (events.contains(val.getKey())) {
EventUtils.RegisterEvent(val.getValue());
}
});
} else {
LogUtils.Info("EventList is empty!");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.v2c.minecraft.plugins.jsonApi.EventNotify.global;

import cn.nukkit.command.ConsoleCommandSender;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
Expand All @@ -13,6 +14,7 @@

public class JsonApiWebSocketServer extends WebSocketServer {
public static HashMap<Object, WebSocket> connPool = new HashMap<Object, WebSocket>();
public static final boolean executeByWs = JsonApi.instance.getConfig().getSection("EventListener").getBoolean("ExecuteByWs");

public JsonApiWebSocketServer(){
super(new InetSocketAddress(JsonApi.instance.getConfig().getSection("Server").getString("IP"), JsonApi.instance.getConfig().getSection("Server").getInt("WsPort")));
Expand All @@ -36,6 +38,9 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {

@Override
public void onMessage(WebSocket conn, String message) {
if(executeByWs){
JsonApi.instance.getServer().dispatchCommand(new ConsoleCommandSender(), message);
}
}

@Override
Expand Down
23 changes: 19 additions & 4 deletions src/tech/v2c/minecraft/plugins/jsonApi/JsonApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import cn.nukkit.plugin.PluginBase;

import org.jline.utils.Log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.nanohttpd.util.ServerRunner;

import tech.v2c.minecraft.plugins.jsonApi.EventNotify.Console.ServerConsoleEvent;
import tech.v2c.minecraft.plugins.jsonApi.EventNotify.Events.*;
import tech.v2c.minecraft.plugins.jsonApi.EventNotify.global.EventManage;
import tech.v2c.minecraft.plugins.jsonApi.EventNotify.global.JsonApiWebSocketServer;
Expand Down Expand Up @@ -39,9 +41,17 @@ public void onEnable() {
LogUtils.Info("JsonAPI Http Server running at: " + getConfig().getSection("Server").getInt("HttpPort"));

if (this.isEnableWs) {
InitEvents();
EventManage.RegisterEventListener();
LogUtils.Info("Finish register events.");
if(getConfig().getSection("EventListener").getBoolean("OtherEvents")){
InitEvents();
EventManage.RegisterEventListener();
LogUtils.Info("Finish register events.");
}

if(getConfig().getSection("EventListener").getBoolean("ServerConsole")){
InitConsoleEvent();
LogUtils.Info("Finish register Server Console Event.");
}

ws = new JsonApiWebSocketServer();
(new Thread(() -> ws.start())).start();
LogUtils.Info("JsonAPI WebSocket Server running at: " + getConfig().getSection("Server").getInt("WsPort"));
Expand Down Expand Up @@ -87,4 +97,9 @@ private void InitPlugin() {
this.isEnableWs = getConfig().getSection("EventListener").getBoolean("IsEnable");
this.isDebugMode = getConfig().getBoolean("DebugMode");
}

private void InitConsoleEvent(){
Logger logger = (Logger)LogManager.getRootLogger();
logger.addAppender(new ServerConsoleEvent());
}
}

0 comments on commit c51e183

Please sign in to comment.