From 5a444ff58015833e029dbc3801f6f0974aeff045 Mon Sep 17 00:00:00 2001 From: ictye <95436661+northgreen@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:05:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=AE=8C=E5=96=84=E7=9A=84=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E7=89=88=E6=9C=AC=E8=BD=AC=E4=B8=BA=E7=A8=B3=E5=AE=9A?= =?UTF-8?q?=E7=89=88=E6=9C=AC=20(#3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更正插件加载方法,允许软件包作为插件加载 * 优化前端效果,使其适配obs * 更新无关紧要的文件的结构 * 1.推进插件接口的完善工作:插件配置 2.推进前端插件工作 * 初步完成前端插件接口的任务,不过还是有点问题难以解决 --- web/js/script/main_js.js | 38 ++++++++++++++--------------- web/js/script/plugin_manager.js | 42 ++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/web/js/script/main_js.js b/web/js/script/main_js.js index 89aefc3..cb79722 100644 --- a/web/js/script/main_js.js +++ b/web/js/script/main_js.js @@ -1,13 +1,13 @@ require.config({ baseUrl:"/" }) -define(["js/plugin/default_plugin_mather","js/script/plugin_manager"], - function ($,pm){ +define(["js/script/plugin_manager"], + function (pm){ function main() { - console.log("lod ready") - //请求ws地址 - console.log(` + console.log("lod ready") + //请求ws地址 + console.log(` __ ___ __ __ __ __ ______ ___ __ /\\ \\__ /\\_ \\ __ /\\ \\ /\\ \\ /\\ \\/\\ \\/\\__ _\\ /\\_ \\ /\\_\\ ___\\ \\ ,_\\ __ __ __ \\//\\ \\ /\\_\\ __ __ __ \\_\\ \\ ___ ___ __ __ __ __\\ \\ \\____ \\ \\ \\ \\ \\/_/\\ \\/ ___ ___ ___ ____ ___\\//\\ \\ __ @@ -19,18 +19,18 @@ define(["js/plugin/default_plugin_mather","js/script/plugin_manager"], \\/__/ `) - //请求websocket - req = new XMLHttpRequest() + //请求websocket + let req = new XMLHttpRequest() - req.open("get","/get_websocket",true) + req.open("get","/get_websocket",true) - req.onreadystatechange=function(){ - if(req.readyState === XMLHttpRequest.DONE && req.status === 200){ - websocket(JSON.parse(req.responseText).local) - } - - } - req.send() + req.onreadystatechange=function(){ + if(req.readyState === XMLHttpRequest.DONE && req.status === 200){ + //启动websocket + websocket(JSON.parse(req.responseText).local) + } + } + req.send() } @@ -40,6 +40,7 @@ function websocket(ura){ var socket = new WebSocket(ura) socket.addEventListener("message",function(event){ console.log('Message from server ', event.data) + if (event.data === "{\"code\": 200, \"msg\": \"connect ok\"}"){ console.info("connect to seriver success") connect_ok = 1 @@ -47,12 +48,9 @@ function websocket(ura){ console.info("cok is "+event.data) let msg = JSON.parse(event.data) if (msg.message_class === "default"){ - let data = $.dm_halder(msg) - if (data !== void 0) { - //TODO:处理非标准消息 - } + //消息处理 + pm.message_handlers(msg) } - } }) socket.onopen=function(){ diff --git a/web/js/script/plugin_manager.js b/web/js/script/plugin_manager.js index e57a077..564a161 100644 --- a/web/js/script/plugin_manager.js +++ b/web/js/script/plugin_manager.js @@ -4,14 +4,13 @@ require.config({ define( function (){ console.info("plugin system is ready") - let plugin_list = plugin_load() - + //消息插件获取 let plugin_load = function (){ let get_plugin = new XMLHttpRequest() let plugin_list = [] get_plugin.open("get","/api/plugin_list",false) - + //回调 get_plugin.onreadystatechange = function () { if (get_plugin.readyState === XMLHttpRequest.DONE && get_plugin.status === 200) { let req = JSON.parse(get_plugin.responseText) @@ -19,29 +18,34 @@ define( } } get_plugin.send() - /* - for(let a in plugin_list){ - let plugin - require([plugin_list[a]],function (plug) { - plugin = plug - }) - plugins[plugin_list[a]] = plugin - } - console.info("az") - * */ - console.info(plugin_list) - return plugin_list - // TODO :剩余工作 } - let message_hader = function (plugin_list) { - //TODO:消息处理器 + let plugin_list = plugin_load() + //消息插件列表 + let plugin_message_handler = {} + //消息插件注册 + for (let x in plugin_list){ + require([plugin_list[x]],function (plugin){ + plugin_message_handler[plugin.message_class] = plugin.dm_halder + }) + } + + /*消息回调*/ + let message_handler = function (message) { + if (Object.keys(plugin_message_handler).length !== 0){ + plugin_message_handler[message.message_class](message) + }else { + // FIXME: 似乎有个难以解决的bug,不过貌似不是很重要,在插件加载之前的消息总是不能被正确处理 + console.debug("a bug?") + console.debug(plugin_message_handler) + } + } return { plugin_load: plugin_load, - message_halders: message_hader, + message_handlers: message_handler, plugin_list: plugin_list } }