mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-20 23:25:35 +08:00
change ALL docs from MkDocs to VuePress!!
This commit is contained in:
@@ -52,10 +52,11 @@ vendor/bin/start server --log-debug # 以 debug 等级启动框架
|
||||
|
||||
输出 warning 级别的 log。
|
||||
|
||||
!!! warning 注意
|
||||
::: warning 注意
|
||||
|
||||
框架内出现的用户态异常,比如无法发送 API、无法连接数据库等错误,都是 warning 错误,不会导致框架崩溃或功能错误的异常情况建议都使用 warning 输出而不是 error。
|
||||
框架内出现的用户态异常,比如无法发送 API、无法连接数据库等错误,都是 warning 错误,不会导致框架崩溃或功能错误的异常情况建议都使用 warning 输出而不是 error。
|
||||
|
||||
:::
|
||||
|
||||
### Console::info()
|
||||
|
||||
@@ -100,9 +101,11 @@ $str = Console::setColor("I am gold color.", "gold");
|
||||
|
||||
炸毛框架支持从终端输入命令来进行一些操作,例如重启框架、停止框架、执行函数等。
|
||||
|
||||
!!! warning 注意
|
||||
::: warning 注意
|
||||
|
||||
在 Docker、systemd、daemon 状态下启动的框架会自动关闭终端等待输入,交互不可用。
|
||||
在 Docker、systemd、daemon 状态下启动的框架会自动关闭终端等待输入,交互不可用。
|
||||
|
||||
:::
|
||||
|
||||
### reload
|
||||
|
||||
@@ -174,4 +177,3 @@ vendor/bin/start server --log-theme={主题名}
|
||||
vendor/bin/start server --log-theme=white-term # 如果用的是白色终端,这个主题更友好
|
||||
vendor/bin/start server --log-theme=no-color # 如果不想让 log 带有任何颜色,使用无色主题
|
||||
```
|
||||
|
||||
|
||||
@@ -40,31 +40,33 @@ public function hello() {
|
||||
|
||||
可以使用的事件:所有 **getFrame()** 可以使用的,`@OnOpenEvent()`,`@OnCloseEvent()`
|
||||
|
||||
!!! tip "提示"
|
||||
::: tip 提示
|
||||
|
||||
值得注意的是,由于机器人客户端和炸毛框架的连接是通过 WebSocket 进行的,而 WebSocket 是长连接,所以同一个机器人一次连接下收发消息所用的连接是同一个,所以 Fd 也是相同的。同理,炸毛框架的内部来区分多个机器人也是通过这一 Fd 进行判定的。
|
||||
值得注意的是,由于机器人客户端和炸毛框架的连接是通过 WebSocket 进行的,而 WebSocket 是长连接,所以同一个机器人一次连接下收发消息所用的连接是同一个,所以 Fd 也是相同的。同理,炸毛框架的内部来区分多个机器人也是通过这一 Fd 进行判定的。
|
||||
|
||||
=== "代码"
|
||||
:::
|
||||
|
||||
```php
|
||||
/**
|
||||
* @CQCommand("测试fd")
|
||||
*/
|
||||
public function testfd() {
|
||||
ctx()->reply("当前机器人连接的fd是:".ctx()->getFd().",机器人QQ是:".ctx()->getRobotId());
|
||||
}
|
||||
```
|
||||
代码
|
||||
|
||||
=== "效果"
|
||||
```php
|
||||
/**
|
||||
* @CQCommand("测试fd")
|
||||
*/
|
||||
public function testfd() {
|
||||
ctx()->reply("当前机器人连接的fd是:".ctx()->getFd().",机器人QQ是:".ctx()->getRobotId());
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
^ 假设我们和连接55555的机器人的私聊
|
||||
) 测试fd
|
||||
( 当前机器人连接的fd是:1,机器人QQ是:55555
|
||||
^ 假设切到了另一个机器人(66666)的私聊
|
||||
) 测试fd
|
||||
( 当前机器人连接的fd是:2,机器人QQ是:66666
|
||||
</chat-box>
|
||||
效果
|
||||
|
||||
<chat-box :my-chats="[
|
||||
{type:2,content:'假设我们和连接55555的机器人的私聊'},
|
||||
{type:0,content:'测试fd'},
|
||||
{type:1,content:'当前机器人连接的fd是:1,机器人QQ是:55555'},
|
||||
{type:2,content:'假设切到了另一个机器人(66666)的私聊'},
|
||||
{type:0,content:'测试fd'},
|
||||
{type:1,content:'当前机器人连接的fd是:2,机器人QQ是:66666'},
|
||||
]"></chat-box>
|
||||
|
||||
## getData() - 获取事件完整数据
|
||||
|
||||
@@ -82,11 +84,11 @@ public function onMessage() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
^ 假设我是QQ为123456的用户,私聊发消息
|
||||
) 哈咯!!
|
||||
( 消息类型是:private
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:2,content:'假设我是QQ为123456的用户,私聊发消息'},
|
||||
{type:0,content:'哈咯!!'},
|
||||
{type:1,content:'消息类型是:private'},
|
||||
]"></chat-box>
|
||||
|
||||
## getRequest() - HTTP 请求对象
|
||||
|
||||
@@ -132,10 +134,10 @@ public function ping() {
|
||||
ctx()->getRobot()->sendPrivateMsg(123456, "发送私聊消息");
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
^ 正在和机器人聊天
|
||||
( 发送私聊消息
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:2,content:'正在和机器人聊天'},
|
||||
{type:1,content:'发送私聊消息'},
|
||||
]"></chat-box>
|
||||
|
||||
## getMessage() - 获取消息
|
||||
|
||||
@@ -143,24 +145,26 @@ ctx()->getRobot()->sendPrivateMsg(123456, "发送私聊消息");
|
||||
|
||||
可以使用的事件:`@CQCommand()`,`@CQMessage`,`@CQBefore("message")`,`@CQAfter("message")`
|
||||
|
||||
=== "代码"
|
||||
```php
|
||||
/**
|
||||
* @CQMessage(group_id=33333)
|
||||
*/
|
||||
public function groupRepeat() {
|
||||
ctx()->reply(ctx()->getMessage());
|
||||
}
|
||||
```
|
||||
代码
|
||||
|
||||
=== "效果"
|
||||
<chat-box>
|
||||
^ 现在在群33333内,机器人已经成了复读机
|
||||
) 来世还做复读机!!!
|
||||
( 来世还做复读机!!!
|
||||
) 你不许复读!
|
||||
( 你不许复读!
|
||||
</chat-box>
|
||||
```php
|
||||
/**
|
||||
* @CQMessage(group_id=33333)
|
||||
*/
|
||||
public function groupRepeat() {
|
||||
ctx()->reply(ctx()->getMessage());
|
||||
}
|
||||
```
|
||||
|
||||
效果
|
||||
|
||||
<chat-box :my-chats="[
|
||||
{type:2,content:'现在在群33333内,机器人已经成了复读机'},
|
||||
{type:0,content:'来世还做复读机!!!'},
|
||||
{type:1,content:'来世还做复读机!!!'},
|
||||
{type:0,content:'你不许复读!'},
|
||||
{type:1,content:'你不许复读!'},
|
||||
]"></chat-box>
|
||||
|
||||
## getUserId() - 获取用户 QQ 号
|
||||
|
||||
@@ -275,16 +279,16 @@ function yourName(){
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) 自我介绍
|
||||
( 你叫啥名字呀?
|
||||
) jerry
|
||||
( 好的,可爱的机器人记住你叫 jerry 啦!以后多聊天哦!
|
||||
) 自我介绍
|
||||
( 你叫啥名字呀?
|
||||
^ 10分钟没理机器人
|
||||
( 你都10分钟不理我了,嘤嘤嘤
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'自我介绍'},
|
||||
{type:1,content:'你叫啥名字呀?'},
|
||||
{type:0,content:'jerry'},
|
||||
{type:1,content:'好的,可爱的机器人记住你叫 jerry 啦!以后多聊天哦!'},
|
||||
{type:0,content:'自我介绍'},
|
||||
{type:1,content:'你叫啥名字呀?'},
|
||||
{type:2,content:'10分钟没理机器人'},
|
||||
{type:1,content:'你都10分钟不理我了,嘤嘤嘤'},
|
||||
]"></chat-box>
|
||||
|
||||
## getArgs() - 自动获取参数
|
||||
|
||||
@@ -316,12 +320,12 @@ public function argTest1() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) test
|
||||
( 请输入你要传入的参数内容
|
||||
) test2
|
||||
( 参数内容:test2
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'test'},
|
||||
{type:1,content:'请输入你要传入的参数内容'},
|
||||
{type:0,content:'test2'},
|
||||
{type:1,content:'参数内容:test2'},
|
||||
]"></chat-box>
|
||||
|
||||
`getArgs()` 也有三层封装,在使用过程中避免麻烦的话,推荐使用下面这几种 `get*Arg()` 方式。
|
||||
|
||||
@@ -341,14 +345,14 @@ public function argTest1() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) test abc def argtest
|
||||
( 参数内容:abc def argtest
|
||||
) test
|
||||
( 请输入你要传入的参数内容
|
||||
) abc def
|
||||
( 参数内容:abc def
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'test abc def argtest'},
|
||||
{type:1,content:'参数内容:abc def argtest'},
|
||||
{type:0,content:'test'},
|
||||
{type:1,content:'请输入你要传入的参数内容'},
|
||||
{type:0,content:'abc def'},
|
||||
{type:1,content:'参数内容:abc def'},
|
||||
]"></chat-box>
|
||||
|
||||
## getNextArg()
|
||||
|
||||
@@ -364,14 +368,14 @@ public function argTest1() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) test abc def argtest
|
||||
( 参数内容:abc
|
||||
) test
|
||||
( 请输入你要传入的参数内容
|
||||
) abc
|
||||
( 参数内容:abc
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'test abc def argtest'},
|
||||
{type:1,content:'参数内容:abc'},
|
||||
{type:0,content:'test'},
|
||||
{type:1,content:'请输入你要传入的参数内容'},
|
||||
{type:0,content:'abc'},
|
||||
{type:1,content:'参数内容:abc'},
|
||||
]"></chat-box>
|
||||
|
||||
## getNumArg()
|
||||
|
||||
@@ -389,14 +393,14 @@ public function argTest1() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) test abc 334 argtest
|
||||
( 数字参数内容:334
|
||||
) test abc
|
||||
( 请输入你要传入的数字内容
|
||||
) 998
|
||||
( 参数内容:998
|
||||
</chat-box>
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'test abc 334 argtest'},
|
||||
{type:1,content:'数字参数内容:334'},
|
||||
{type:0,content:'test abc'},
|
||||
{type:1,content:'请输入你要传入的参数内容'},
|
||||
{type:0,content:'998'},
|
||||
{type:1,content:'参数内容:998'},
|
||||
]"></chat-box>
|
||||
|
||||
## copy()
|
||||
|
||||
@@ -418,8 +422,7 @@ public function argTest1() {
|
||||
}
|
||||
```
|
||||
|
||||
<chat-box>
|
||||
) test abc 334 argtest
|
||||
( 参数内容:abc, 334, argtest
|
||||
</chat-box>
|
||||
|
||||
<chat-box :my-chats="[
|
||||
{type:0,content:'test abc 334 argtest'},
|
||||
{type:1,content:'参数内容:abc, 334, argtest'},
|
||||
]"></chat-box>
|
||||
|
||||
@@ -60,4 +60,3 @@ for($i = 0; $i < 1000; ++$i) {
|
||||
`$name` 为字符串,是你要用的协程池的名称。
|
||||
|
||||
`$size` 为大小,最大不可超过 Swoole 配置文件中指定的最大协程数量。
|
||||
|
||||
|
||||
@@ -101,4 +101,4 @@ public function randNum() {
|
||||
|
||||
## EventDispatcher::enableEventTrace() - 启用事件跟踪器
|
||||
|
||||
还没写完,不着急。
|
||||
还没写完,不着急。
|
||||
|
||||
@@ -8,35 +8,35 @@
|
||||
|
||||
根据加载的用户编写的代码类名来获取类所在的文件路径。
|
||||
|
||||
=== "src/Module/Example/Hello.php"
|
||||
**src/Module/Example/Hello.php**
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace Module\Example;
|
||||
class Hello { ... }
|
||||
```
|
||||
```php
|
||||
<?php
|
||||
namespace Module\Example;
|
||||
class Hello { ... }
|
||||
```
|
||||
|
||||
=== "src/Module/Example/Start.php"
|
||||
**src/Module/Example/Start.php**
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace Module\Example;
|
||||
use ZM\Annotation\Swoole\OnStart;
|
||||
class Start {
|
||||
/**
|
||||
* @OnStart()
|
||||
*/
|
||||
public function onStart() {
|
||||
Console::info("Path: ".getClassPath(Hello::class));
|
||||
}
|
||||
}
|
||||
```
|
||||
```php
|
||||
<?php
|
||||
namespace Module\Example;
|
||||
use ZM\Annotation\Swoole\OnStart;
|
||||
class Start {
|
||||
/**
|
||||
* @OnStart()
|
||||
*/
|
||||
public function onStart() {
|
||||
Console::info("Path: ".getClassPath(Hello::class));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "输出结果"
|
||||
**输出结果**
|
||||
|
||||
```
|
||||
[11:12:02] [I] [#0] Path: /mnt/d/project/zhamao-framework/src/Module/Example/Hello.ph
|
||||
```
|
||||
```
|
||||
[11:12:02] [I] [#0] Path: /mnt/d/project/zhamao-framework/src/Module/Example/Hello.ph
|
||||
```
|
||||
|
||||
## explodeMsg()
|
||||
|
||||
@@ -289,7 +289,7 @@ zm_dump($pass);
|
||||
|
||||
定义:`zm_config($name, $key = null)`。
|
||||
|
||||
有关 ZMConfig 模块的说明,见 [指南 - 基本配置](/guide/basic-config/)。
|
||||
有关 ZMConfig 模块的说明,见 [指南 - 基本配置](/guide/basic-config)。
|
||||
|
||||
```php
|
||||
zm_config("global"); //等同于 ZMConfig::get("global");
|
||||
@@ -321,4 +321,3 @@ zm_config("global", "swoole"); //等同于 ZMConfig::get("global", "swoole");
|
||||
## zm_verbose()
|
||||
|
||||
同 `Console::verbose($msg)`。
|
||||
|
||||
|
||||
@@ -1,37 +1,44 @@
|
||||
# 远程终端
|
||||
|
||||
框架在 2.3 版本时删除了本地终端(就是框架启动后可以在终端输入一些参数),因为框架的多进程模式会导致终端输入错乱,所以暂时取消掉了。
|
||||
|
||||
而远程终端应运而生,为的是弥补这一功能。与之前不同的是,远程终端使用 nc 连接,无需任何其他组件和客户端,而且功能更丰富,支持自定义命令。
|
||||
|
||||
## 启用
|
||||
|
||||
有两种开启方式:
|
||||
|
||||
- 永久开启:全局配置文件中找到 `remote_terminal` 的 `status`,改为 true,启动框架即可。
|
||||
- 临时开启:启动框架时加上参数 `--remote-terminal`。例如:`vendor/bin/start server --remote-terminal`。
|
||||
|
||||
## 配置
|
||||
|
||||
在一般情况下,框架为了安全,直接按照默认配置,会监听 `127.0.0.1:20002` 端口,不可以远程访问,只能使用本机的 nc 连接,效果如下:
|
||||
|
||||
本地主机:
|
||||
|
||||

|
||||
|
||||
从别的主机:
|
||||
|
||||

|
||||
|
||||
如果将 `host` 改为 `0.0.0.0` 或对应监听地址,即可指向性访问。
|
||||
|
||||
但是,如果你又想远程连接,又想保证安全,那么可以设置一个 token 参数,来保证连接时需要输入 token 才能使用远程终端。
|
||||
假设我们的 token 是 `iAMTokEn`:
|
||||

|
||||
|
||||

|
||||
|
||||
## 使用
|
||||
|
||||
默认情况下,使用 `nc` 命令即可。
|
||||
|
||||
```bash
|
||||
nc <your-host> <your-port> -vvv
|
||||
# nc 127.0.0.1 20002 -vvv
|
||||
```
|
||||
|
||||
输入 help 即可查看内置的常用指令:
|
||||

|
||||
|
||||

|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# 单例类(SingletonTrait)
|
||||
# 单例类 - SingletonTrait
|
||||
|
||||
单例类,顾名思义,就是让用户声明的类拥有单例的特性,而这一组件引入的方式也最直接。它是一个 PHP 的 `trait`。
|
||||
|
||||
@@ -40,4 +40,4 @@ Foo::getInstance()->test = 5;
|
||||
var_dump(Foo::getInstance()->test);
|
||||
```
|
||||
|
||||
只需要在类中使用:`use \ZM\Utils\SingletonTrait;` 一句话即可。
|
||||
只需要在类中使用:`use \ZM\Utils\SingletonTrait;` 一句话即可。
|
||||
|
||||
@@ -23,4 +23,3 @@
|
||||
```php
|
||||
TaskManager::runTask("heavy_task", 100, "param1", "param2");
|
||||
```
|
||||
|
||||
|
||||
@@ -64,4 +64,3 @@ array:31 [
|
||||
```
|
||||
|
||||
> 为什么不能重载所有文件?因为框架是多进程模型,而重载相当于只重新启动了一次 Worker 进程,Manager 和 Master 进程未重启,所以被 Manager、Master 进程已经加载的 PHP 文件无法使用 reload 命令重新加载。详见 [进阶 - 进程间隔离](/advanced/multi-process/#_5)。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user