diff --git a/README.md b/README.md index 799ebb42..905b33c3 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![zhamao License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/zhamao-robot/zhamao-framework/blob/master/LICENSE) [![Latest Stable Version](http://img.shields.io/packagist/v/zhamao/framework.svg)](https://packagist.org/packages/zhamao/framework) [![Banner](https://img.shields.io/badge/CQHTTP-v11-black)]() -[![dev-version](https://img.shields.io/badge/dev--version-v2.0.0--a1-green)]() +[![dev-version](https://img.shields.io/badge/dev--version-v2.0.0--beta1-green)]() [![stupid counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/stupid.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=stupid) [![TODO counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/TODO.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=TODO) diff --git a/composer.json b/composer.json index 7b0b8bce..29aa2d21 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "High performance QQ robot and web server development framework", "minimum-stability": "stable", "license": "Apache-2.0", - "version": "2.0.0-a2", + "version": "2.0.0-b1", "authors": [ { "name": "whale", diff --git a/config/global.php b/config/global.php index 07c3d9e9..00bffce1 100644 --- a/config/global.php +++ b/config/global.php @@ -27,7 +27,7 @@ $config['crash_dir'] = $config['zm_data'] . 'crash/'; /** 对应swoole的server->set参数 */ $config['swoole'] = [ 'log_file' => $config['crash_dir'] . 'swoole_error.log', - 'worker_num' => swoole_cpu_num(), + 'worker_num' => swoole_cpu_num(), //如果你只有一个 OneBot 实例连接到框架并且代码没有复杂的CPU密集计算,则可把这里改为1使用全局变量 'dispatch_mode' => 2, //包分配原则,见 https://wiki.swoole.com/#/server/setting?id=dispatch_mode 'max_coroutine' => 300000, //'task_worker_num' => 4, @@ -115,7 +115,7 @@ $config['command_register_class'] = [ /** 服务器启用的外部第三方和内部插件 */ $config['modules'] = [ - 'qqbot' => true, // QQ机器人事件解析器,如果取消此项则默认为 true 开启状态,否则你手动填写 false 才会关闭 + 'onebot' => true, // QQ机器人事件解析器,如果取消此项则默认为 true 开启状态,否则你手动填写 false 才会关闭 ]; return $config; diff --git a/src/Module/Example/Hello.php b/src/Module/Example/Hello.php index 99c49bab..40a41545 100644 --- a/src/Module/Example/Hello.php +++ b/src/Module/Example/Hello.php @@ -2,12 +2,13 @@ namespace Module\Example; +use ZM\Annotation\Http\Middleware; use ZM\Annotation\Swoole\OnSwooleEvent; use ZM\ConnectionManager\ConnectionObject; use ZM\Console\Console; use ZM\Annotation\CQ\CQCommand; use ZM\Annotation\Http\RequestMapping; -use ZM\Utils\ZMUtil; +use ZM\Store\Redis\ZMRedis; /** * Class Hello @@ -17,25 +18,38 @@ use ZM\Utils\ZMUtil; class Hello { /** - * 在机器人连接后向终端输出信息 - * @OnSwooleEvent("open",rule="connectIsQQ()") - * @param $conn + * 一个简单的redis连接池使用demo,将下方user_id改为你自己的QQ号即可(为了不被不法分子利用) + * @CQCommand("redis_test",user_id=627577391) */ - public function onConnect(ConnectionObject $conn) { - Console::info("机器人 " . $conn->getOption("connect_id") . " 已连接!"); + public function testCase() { + $a = new ZMRedis(); + $redis = $a->get(); + $r1 = ctx()->getArgs(ZM_MATCH_FIRST, "请说出你想设置的操作[r/w]"); + switch ($r1) { + case "r": + $k = ctx()->getArgs(ZM_MATCH_FIRST, "请说出你想读取的键名"); + $result = $redis->get($k); + ctx()->reply("结果:" . $result); + break; + case "w": + $k = ctx()->getArgs(ZM_MATCH_FIRST, "请说出你想写入的键名"); + $v = ctx()->getArgs(ZM_MATCH_FIRST, "请说出你想写入的字符串"); + $result = $redis->set($k, $v); + ctx()->reply("结果:" . ($result ? "成功" : "失败")); + break; + } } /** - * 在机器人断开连接后向终端输出信息 - * @OnSwooleEvent("close",rule="connectIsQQ()") - * @param ConnectionObject $conn + * @CQCommand("我是谁") */ - public function onDisconnect(ConnectionObject $conn) { - Console::info("机器人 " . $conn->getOption("connect_id") . " 已断开连接!"); + public function whoami() { + $user = ctx()->getRobot()->setCallback(true)->getLoginInfo(); + return "你是" . $user["data"]["nickname"] . ",QQ号是" . $user["data"]["user_id"]; } /** - * 向机器人发送"你好",即可回复这句话 + * 向机器人发送"你好啊",也可回复这句话 * @CQCommand(match="你好",alias={"你好啊","你是谁"}) */ public function hello() { @@ -43,14 +57,9 @@ class Hello } /** - * @CQCommand(".reload") - */ - public function reload() { - context()->reply("reloading..."); - ZMUtil::reload(); - } - - /** + * 一个简单随机数的功能demo + * 问法1:随机数 1 20 + * 问法2:从1到20的随机数 * @CQCommand("随机数") * @CQCommand(pattern="*从*到*的随机数") * @return string @@ -69,6 +78,7 @@ class Hello /** * 中间件测试的一个示例函数 * @RequestMapping("/httpTimer") + * @Middleware("timer") */ public function timer() { return "This page is used as testing TimerMiddleware! Do not use it in production."; @@ -93,6 +103,24 @@ class Hello return "Your name: {$param["name"]}"; } + /** + * 在机器人连接后向终端输出信息 + * @OnSwooleEvent("open",rule="connectIsQQ()") + * @param $conn + */ + public function onConnect(ConnectionObject $conn) { + Console::info("机器人 " . $conn->getOption("connect_id") . " 已连接!"); + } + + /** + * 在机器人断开连接后向终端输出信息 + * @OnSwooleEvent("close",rule="connectIsQQ()") + * @param ConnectionObject $conn + */ + public function onDisconnect(ConnectionObject $conn) { + Console::info("机器人 " . $conn->getOption("connect_id") . " 已断开连接!"); + } + /** * 框架会默认关闭未知的WebSocket链接,因为这个绑定的事件,你可以根据你自己的需求进行修改 * @OnSwooleEvent(type="open",rule="connectIsDefault()") diff --git a/src/ZM/Context/Context.php b/src/ZM/Context/Context.php index c158330f..c5cc0c0f 100644 --- a/src/ZM/Context/Context.php +++ b/src/ZM/Context/Context.php @@ -5,6 +5,7 @@ namespace ZM\Context; use Co; +use Exception; use Swoole\Http\Request; use Swoole\WebSocket\Frame; use swoole_server; @@ -15,9 +16,6 @@ use ZM\Exception\InvalidArgumentException; use ZM\Exception\WaitTimeoutException; use ZM\Http\Response; use ZM\API\ZMRobot; -use ZM\Store\LightCacheInside; -use ZM\Store\Lock\SpinLock; -use ZM\Store\ZMAtomic; use ZM\Utils\CoMessage; class Context implements ContextInterface @@ -146,7 +144,11 @@ class Context implements ContextInterface Console::debug("==== 开始等待输入 ===="); if ($prompt != "") $this->reply($prompt); - $r = CoMessage::yieldByWS($this->getData(), ["user_id", "self_id", "message_type", onebot_target_id_name($this->getMessageType())]); + try { + $r = CoMessage::yieldByWS($this->getData(), ["user_id", "self_id", "message_type", onebot_target_id_name($this->getMessageType())]); + } catch (Exception $e) { + $r = false; + } if($r === false) { throw new WaitTimeoutException($this, $timeout_prompt); } @@ -231,4 +233,6 @@ class Context implements ContextInterface } public function copy() { return self::$context[$this->cid]; } + + public function getOption() { return self::getCache("match"); } } diff --git a/src/ZM/Context/ContextInterface.php b/src/ZM/Context/ContextInterface.php index 0f76be9d..94689d37 100644 --- a/src/ZM/Context/ContextInterface.php +++ b/src/ZM/Context/ContextInterface.php @@ -114,4 +114,6 @@ interface ContextInterface public function cloneFromParent(); public function copy(); + + public function getOption(); } diff --git a/src/ZM/Event/ServerEventHandler.php b/src/ZM/Event/ServerEventHandler.php index 42637178..6783e1be 100644 --- a/src/ZM/Event/ServerEventHandler.php +++ b/src/ZM/Event/ServerEventHandler.php @@ -134,10 +134,7 @@ class ServerEventHandler foreach ($server->connections as $v) { $server->close($v); } - if (SqlPoolStorage::$sql_pool !== null) { - SqlPoolStorage::$sql_pool->close(); - SqlPoolStorage::$sql_pool = null; - } + // 这里执行的是只需要执行一遍的代码,比如终端监听器和键盘监听器 /*if ($server->worker_id === 0) { @@ -163,6 +160,10 @@ class ServerEventHandler }*/ //TODO: 单独抽出来MySQL和Redis连接池 if (ZMConfig::get("global", "sql_config")["sql_host"] != "") { + if (SqlPoolStorage::$sql_pool !== null) { + SqlPoolStorage::$sql_pool->close(); + SqlPoolStorage::$sql_pool = null; + } Console::info("新建SQL连接池中"); ob_start(); phpinfo(); @@ -503,9 +504,9 @@ class ServerEventHandler //加载插件 $plugins = ZMConfig::get("global", "modules") ?? []; - if (!isset($plugins["qqbot"])) $plugins["qqbot"] = true; + if (!isset($plugins["onebot"])) $plugins["onebot"] = true; - if ($plugins["qqbot"]) { + if ($plugins["onebot"]) { $obj = new OnSwooleEvent(); $obj->class = QQBot::class; $obj->method = 'handle'; @@ -516,7 +517,7 @@ class ServerEventHandler } //TODO: 编写加载外部插件的方式 - //$this->loadExternalModules(); + $this->loadExternalModules($plugins); } private function addWatcher($maindir, $fd) { @@ -530,4 +531,11 @@ class ServerEventHandler } } } + + private function loadExternalModules($plugins) { + foreach ($plugins as $k => $v) { + if ($k == "onebot") continue; + + } + } } diff --git a/src/ZM/Module/QQBot.php b/src/ZM/Module/QQBot.php index e77d2ff1..13f75c7d 100644 --- a/src/ZM/Module/QQBot.php +++ b/src/ZM/Module/QQBot.php @@ -21,7 +21,7 @@ use ZM\Utils\CoMessage; /** * Class QQBot * @package ZM\Module - * @ExternalModule("qqbot") + * @ExternalModule("onebot") */ class QQBot { diff --git a/test/ZMTest/Mock/global.php b/test/ZMTest/Mock/global.php index c2f5915c..e076b681 100644 --- a/test/ZMTest/Mock/global.php +++ b/test/ZMTest/Mock/global.php @@ -105,7 +105,7 @@ $config['command_register_class'] = [ /** 服务器启用的外部第三方和内部插件 */ $config['modules'] = [ - 'qqbot' => true, // QQ机器人事件解析器,如果取消此项则默认为 true 开启状态,否则你手动填写 false 才会关闭 + 'onebot' => true, // QQ机器人事件解析器,如果取消此项则默认为 true 开启状态,否则你手动填写 false 才会关闭 ]; return $config;