modified: main.js
modified: tools/pluginManager.js modified: tools/websocketManager.js
This commit is contained in:
22
main.js
22
main.js
@@ -2,10 +2,20 @@ const settingsManager = require("./tools/settingsManager");
|
||||
const webSocketManager = require("./tools/websocketManager");
|
||||
const pluginManager = require("./tools/pluginManager");
|
||||
|
||||
var wsm = new webSocketManager({ port: 8080 });
|
||||
var pluginsManager = new pluginManager(__dirname);
|
||||
class main {
|
||||
constructor(args = process.argv.slice(2)) {
|
||||
this.wsm = new webSocketManager({ port: 8080 });
|
||||
this.plugins = new pluginManager(__dirname, this.wsm);
|
||||
this.#Main(args);
|
||||
}
|
||||
|
||||
pluginsManager.pluginList.forEach(pluginData => {
|
||||
var plugin = require(pluginData.pluginPath)
|
||||
var pluginInstance = new plugin(pluginData.settings);
|
||||
});
|
||||
#Main(args) { }
|
||||
}
|
||||
|
||||
new main();
|
||||
|
||||
|
||||
/*
|
||||
TODO:
|
||||
make plugins expose their available methods and settings to the dashboard
|
||||
*/
|
||||
@@ -4,7 +4,8 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = class pluginManager {
|
||||
constructor(baseDir) {
|
||||
constructor(baseDir, wsm) {
|
||||
this.wsm = wsm;
|
||||
this.pluginsPath = path.join(baseDir, "plugins");
|
||||
this.pluginList = [];
|
||||
this.#scanPluginsFolder();
|
||||
@@ -13,15 +14,15 @@ module.exports = class pluginManager {
|
||||
|
||||
#scanPluginsFolder() {
|
||||
try {
|
||||
let files = fs.readdirSync(this.pluginsPath);
|
||||
files.forEach((file) => {
|
||||
let filePath = path.join(this.pluginsPath, file, "pluginData.json");
|
||||
let pluginsFolder = fs.readdirSync(this.pluginsPath);
|
||||
pluginsFolder.forEach((pluginFolder) => {
|
||||
let filePath = path.join(this.pluginsPath, pluginFolder, "pluginData.json");
|
||||
if (fs.existsSync(filePath)) {
|
||||
let pluginData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
this.#checkDependencies(pluginData);
|
||||
this.#loadPlugin(path.join(this.pluginsPath, file, pluginData.entrypoint), pluginData);
|
||||
this.#loadPlugin(path.join(this.pluginsPath, pluginFolder, pluginData.entrypoint), pluginData);
|
||||
} else {
|
||||
console.log(`No pluginData.json found for ${file}`);
|
||||
console.log(`No pluginData.json found for ${pluginFolder}`);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -55,6 +56,6 @@ module.exports = class pluginManager {
|
||||
|
||||
#loadPlugin(pluginPath, pluginData) {
|
||||
let plugin = require(pluginPath)
|
||||
this.pluginList.push(new plugin(pluginData));
|
||||
this.pluginList.push(new plugin(pluginData, this.wsm));
|
||||
}
|
||||
}
|
||||
@@ -8,33 +8,33 @@ module.exports = class webSocketManager {
|
||||
}
|
||||
|
||||
#initializeWS(settings) {
|
||||
this.wsServer.on('connection', function connection(ws, req) {
|
||||
this.wsServer.on('connection', (ws, req) => {
|
||||
let ip = req.socket.remoteAddress;
|
||||
let protocol = req.headers['sec-websocket-protocol'];
|
||||
console.log(clc.greenBright(`+client ${ip} with protocol: ${protocol} Connected `));
|
||||
//ws.on("close", onCloseHandler);
|
||||
ws.on("close", () => {
|
||||
console.log(clc.redBright(`-client ${ip} with protocol: ${protocol} Disconnected`));
|
||||
});
|
||||
ws.on("close", () => this.#onClose(ip, protocol));
|
||||
|
||||
ws.on("message", (message) => {
|
||||
//receiver_handle(message, req);
|
||||
});
|
||||
ws.on("message", (message) => this.#onMessage(ip, protocol, message));
|
||||
|
||||
ws.onerror = function (error) {
|
||||
ws.onerror = (error) => {
|
||||
console.log(`Error: ${error.message}`);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
#onClose(ip, protocol) {
|
||||
console.log(clc.redBright(`-client ${ip} with protocol: ${protocol} Disconnected`));
|
||||
}
|
||||
#onMessage(ip, protocol, message) { }
|
||||
|
||||
broadcastMessage(message, target = "all") {
|
||||
this.wsServer.clients.forEach(function each(client) {
|
||||
if (client.readyState === WebSocketServer.OPEN) {
|
||||
if (client.protocol == target || target == "all") {
|
||||
this.wsServer.clients.forEach((client) => {
|
||||
if (client.readyState === ws.OPEN) {
|
||||
if (client.protocol === target || target === "all") {
|
||||
client.send(message);
|
||||
console.log(`Sent message: ${msg} to ${client._socket.remoteAddress} with protocol: ${client.protocol}`);
|
||||
console.log(`Sent message: ${message} to ${client._socket.remoteAddress} with protocol: ${client.protocol}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user