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 } }