Skip to content

Commit

Permalink
Function return value as RPC call return value
Browse files Browse the repository at this point in the history
  • Loading branch information
h5mcbox committed Feb 28, 2024
1 parent da33fc7 commit 593195e
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 202 deletions.
64 changes: 0 additions & 64 deletions model/Observer.js

This file was deleted.

23 changes: 23 additions & 0 deletions model/RPCHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable no-prototype-builtins */

module.exports = class RPCHandler {
#routes = new Map;
//监听
define(event, _, callback = null) {
if (callback === null) {
callback = _; _ = null;
}
let callbackConfig = { callback };
this.#routes.set(event, callbackConfig);
};
//触发
async emit(event, ...args) {
if (!this.#routes.has(event)) return [false, null];
let { callback } = this.#routes.get(event);
return [true, await callback(...args)];
}
//移除监听
remove(event) {
this.#routes.delete(event);
};
}
13 changes: 7 additions & 6 deletions model/WebSocketModel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { wsResponseError } = require("../helper/Response");
const Observer = require("./Observer");
const { wsResponseError, wsResponse } = require("../helper/Response");
const RPCHandler = require("./RPCHandler");

const WebSocketModel = new Observer;
const WebSocketModel = new RPCHandler;

//事件二次转发 监听ws/req即可监听所有Websocket请求
WebSocketModel.listener("ws/req", "", data => {
WebSocketModel.define("ws/req", "", async data => {
try {
let result=WebSocketModel.emit(data.header.RequestKey, data);
if(!result)throw "Method not found";
let [success, result] = await WebSocketModel.emit(data.header.RequestKey, data);
wsResponse(data, result);
if (!success) throw "Method not found";
} catch (err) {
wsResponseError(data, err);
throw err;
Expand Down
16 changes: 6 additions & 10 deletions route/websocket/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,34 @@ setInterval(function () {
}, MCSERVER.localProperty.data_center_times);

//重启逻辑
WebSocketObserver().listener("center/restart", data => {
WebSocketObserver().define("center/restart", data => {
MCSERVER.log("Worker重启...");
response.wsResponse(data, true);
process.nextTick(() => {
return process.nextTick(() => {
process.send({ restart: "./app.js" });
process.emit("SIGINT");
});
});

//更新逻辑
WebSocketObserver().listener("center/update", async data => {
WebSocketObserver().define("center/update", async data => {
let { sign: remoteSign, buffer } = data.body;
let now = Math.floor(Date.now() / 1000);
let timeWindow = Math.floor(now / 120);
let timeKey = hash.hmac(MCSERVER.localProperty.MasterKey, timeWindow.toString());
let sign = hash.hmac(timeKey, buffer);
if (sign !== remoteSign) response.wsResponse(data, false);
if (sign !== remoteSign) return false;

const target_path = "./app.apkg";
fs.writeFileSync("./app.backup.apkg", fs.readFileSync(target_path))
fs.writeFileSync(target_path, buffer);
MCSERVER.log("[ 软件更新 ] Backend执行软件更新");

MCSERVER.log("Worker重启...");
response.wsResponse(data, true);
process.nextTick(() => {
return process.nextTick(() => {
process.send({ restart: "./app.js" });
process.emit("SIGINT");
});
});

//数据中心
WebSocketObserver().listener("center/show", data => {
response.wsResponse(data, cacheSystemInfo);
});
WebSocketObserver().define("center/show", data => cacheSystemInfo);
10 changes: 5 additions & 5 deletions route/websocket/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ serverModel.ServerManager().on("open", data => {
});

//控制请求监听控制台实例
WebSocketObserver().listener("server/console/ws", data => {
WebSocketObserver().define("server/console/ws", data => {
let serverName = data.body.trim();

MCSERVER.log("[" + serverName + "] >>> 准许控制台监听");
Expand All @@ -77,20 +77,20 @@ WebSocketObserver().listener("server/console/ws", data => {
// 重置用户历史指针
const instanceLogHistory = serverModel.ServerManager().getServer(serverName).logHistory;
if (instanceLogHistory) instanceLogHistory.setPoint("", 0);
return response.wsResponse(data,null);
return null;
});

//前端退出控制台界面
WebSocketObserver().listener("server/console/remove", data => {
WebSocketObserver().define("server/console/remove", data => {
//单页退出时触发
var serverName = data.body.trim();
for (let client of Object.values(MCSERVER.allSockets)) {
if (client.console === serverName) {
client.console = undefined;
return response.wsResponse(data,true);
return true;
}
}
return response.wsResponse(data,false);
return false;
});

// 缓冲区定时发送频率,默认限制两秒刷新缓冲区
Expand Down
52 changes: 26 additions & 26 deletions route/websocket/console/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ const permssion = require("../../../helper/Permission");
const mcPingProtocol = require("../../../helper/MCPingProtocol");

//发送指令
WebSocketObserver().listener("server/console/command", data => {
WebSocketObserver().define("server/console/command", data => {
let par = data.body;
let serverName = par.serverName.trim();
let command = par.command;
//代表重启服务器
if (command == "__restart__") {
serverModel.ServerManager().getServer(serverName).restart();
response.wsMsgWindow(data.ws, "服务器正在重启..");
return response.wsResponse(data, false);
}
//强制结束服务端进程
if (command == "__killserver__") {
serverModel.ServerManager().getServer(serverName).kill();
response.wsMsgWindow(data.ws, "服务器结束进程");
return response.wsResponse(data, false);
}
//通过命令方法停止服务端
if (command == "__stop__") {
let server = serverModel.ServerManager().getServer(serverName);
let isRestart = server.dataModel.autoRestart;
if (isRestart) {
server.dataModel.autoRestart = false;
server._onceStopRestart = true;
}
server.stopServer();
return response.wsResponse(data, false);
//代表重启服务器
if (command == "__restart__") {
serverModel.ServerManager().getServer(serverName).restart();
response.wsMsgWindow(data.ws, "服务器正在重启..");
return false;
}
//强制结束服务端进程
if (command == "__killserver__") {
serverModel.ServerManager().getServer(serverName).kill();
response.wsMsgWindow(data.ws, "服务器结束进程");
return false;
}
//通过命令方法停止服务端
if (command == "__stop__") {
let server = serverModel.ServerManager().getServer(serverName);
let isRestart = server.dataModel.autoRestart;
if (isRestart) {
server.dataModel.autoRestart = false;
server._onceStopRestart = true;
}
//不是特殊命令,则直接执行
serverModel.sendCommand(serverName, command);
return response.wsResponse(data, false);
server.stopServer();
return false;
}
//不是特殊命令,则直接执行
serverModel.sendCommand(serverName, command);
return false;
});

//服务端退出之后第一事件
Expand Down
6 changes: 3 additions & 3 deletions route/websocket/console/console-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const os = require("os");
const mcPingProtocol = require("../../../helper/MCPingProtocol");

//控制台信息获取
WebSocketObserver().listener("server/console", data => {
WebSocketObserver().define("server/console", data => {
// permssion.needLogin(req, res);
let serverName = data.body.trim();
let serverData = serverModel.ServerManager().getServer(serverName);
let sysMonery = ((os.freemem() / 1024 / (os.totalmem() / 1024)) * 100).toFixed(2);
// let cpu = MCSERVER.dataCenter.cacheCPU;
response.wsResponse(data, {
return {
serverData: serverData.dataModel,
run: serverData.isRun(),
sysMonery: sysMonery,
Expand All @@ -24,5 +24,5 @@ WebSocketObserver().listener("server/console", data => {
current_players: "--",
max_players: "--"
}
});
};
});
9 changes: 4 additions & 5 deletions route/websocket/console/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ const response = require("../../../helper/Response");
const HISTORY_SIZE_LINE = 1024;

// 正序历史记录路由
WebSocketObserver().listener("server/console/history", data => {
WebSocketObserver().define("server/console/history", data => {
let bodyJson = data.body;
let serverName = bodyJson["serverName"] || "";
const logHistory = serverModel.ServerManager().getServer(serverName).logHistory;
if (!logHistory) {
response.wsResponse(data, "[控制面板]: 暂无任何历史记录.\r\n");
return;
return "[控制面板]: 暂无任何历史记录.\r\n";
}
logHistory.readLine("", HISTORY_SIZE_LINE, (sendText) => {
if (sendText) {
sendText = sendText.replace(/\n/gim, "\r\n");
sendText = sendText.replace(/\r\r\n/gim, "\r\n");
response.wsResponse(data, sendText);
return sendText;
} else {
response.wsResponse(data, "[控制面板]: 无法再读取更多的服务端日志.\r\n");
return "[控制面板]: 无法再读取更多的服务端日志.\r\n";
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions route/websocket/console/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { WebSocketObserver } = require("../../../model/WebSocketModel");
const mcPingProtocol = require("../../../helper/MCPingProtocol");

//开启服务器
WebSocketObserver().listener("server/console/open", data => {
WebSocketObserver().define("server/console/open", data => {
let serverName = data.body.trim();
MCSERVER.log("Backend 正在启动 ", serverName, " 服务端实例...");
try {
Expand All @@ -13,15 +13,15 @@ WebSocketObserver().listener("server/console/open", data => {
response.wsMsgWindow(data.ws, "服务器无法启动,建议检查配置或权限");
return;
}
response.wsResponse(data, true);
// 传递开启服务端事件
serverModel.ServerManager().emit("open_next", {
serverName: serverName
});
return true;
} catch (err) {
response.wsMsgWindow(data.ws, "" + err);
}
return response.wsResponse(data, "server/console/open");
return null;
});

// 服务端开启后的第一事件
Expand Down
Loading

0 comments on commit 593195e

Please sign in to comment.