ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# Client模块 [![](https://img.shields.io/badge/simple--robot--module-debugger-green)](https://github.com/ForteScarlet/simple-robot-core) [![](https://img.shields.io/maven-central/v/love.forte.simple-robot-module/simple-robot-module-debugger-client)](https://search.maven.org/artifact/love.forte.simple-robot-module/simple-robot-module-debugger-client) 依赖于Common模块,在程序启动后启动一个socket客户端来支持远程debug。其对应的服务端为前章节介绍到的`server`模块。 >[info] client 模块一般用于通过配合server模块来进行远程debug。 <br> ## **简介** client会在启动的时候去连接配置中指定地址的server。 当client连接后,便可以通过client向server提交事件并触发server端的监听事件,以实现远程通过client对server进行调试。 当server端的配置中的`simbot.module.debugger.listen=true`的时候,server中所触发的其他非debug监听事件的时候,会将此事件提交给client,实现通过client监听server端的事件。 但是需要注意的是,不论事件提交为`client -> server`还是`server -> client`,他们所接收到的`MsgGet`实例永远是`debugger-common`模块中提供的debugger特供的实例,不属于任何组件。 一个server可连接多个client。 <br> ## **使用** ### **① 构建项目** 以maven为例: (版本参考maven仓库或者此文档上方的版本号图标) ```xml <!-- https://search.maven.org/artifact/love.forte.simple-robot-module/simple-robot-module-debugger-client--> <dependency> <groupId>love.forte.simple-robot-module</groupId> <artifactId>simple-robot-module-debugger-client</artifactId> <version>${project.version}</version> </dependency> ``` <br> ### **② 配置文件** ```properties # suppress inspection "UnusedProperty" for whole file # debugger client # debugger client连接的server ip, 默认127.0.0.1 simbot.module.debugger.host=forte.love # debugger client连接的server 端口,默认9998 simbot.module.debugger.port=9998 ``` <br> ### **③ 使用** 一般使用client的时候是配合`debugger-common`的`DebugApplication`启动器进行离线测试。 ```java // 如果不用接口启动,则记得标注注解。默认的配置文件的文件名为:simple-robot-conf.properties @SimpleRobotApplication public class DebugClientRunApplication { public static void main(String[] args) throws InterruptedException { DebugContext context = new DebugApplication().run(DebugClientRunApplication.class, args); // 看看 debugger 注册了什么bot info BotManager botManager = context.getBotManager(); for (BotInfo bot : botManager.bots()) { System.out.println(bot.getSender().GETTER.getLoginQQInfo()); } // 获取client中的控制器,功能与server中的控制台一致,用于向server端提交事件。 DebugController controller = context.getController(); // 如果连接存活,则每2秒发送一次随机的私信消息。 while(controller.isActive()){ controller.send(MsgGetTypes.privateMsg, msg -> { // 将msg中的thisCode设置为统一的账号:114514 // 实际情况下应该设置为与你的远程端一致的账号信息 msg.setDgThisCode("114514"); // msg.setThisCode("114514"); // 上下两个方法一样效果 }); System.out.println("send private msg"); TimeUnit.SECONDS.toMillis(2); } // 连接断开则会跳出循环 System.out.println("out - not active"); } } ``` >[info] 当server端被关闭的时候,client则会断开。