远程退出
概述
远程退出系统处理服务器发送的踢出或退出指令。当服务器需要强制用户下线时(如账号异地登录、违规操作、服务器维护等),会发送相应指令。
退出类型
| 常量 | 值 | 说明 | 典型场景 |
|---|---|---|---|
TXNET_EXIT_KICK | 0 | 被踢出 | 账号异地登录、违规操作、管理员踢出 |
TXNET_EXIT_EXIT | 1 | 服务器退出指令 | 服务器维护、版本过期、紧急下线 |
运行模式
通过 txnet_set_exit_mode 设置:
| 模式 | 值 | 行为 |
|---|---|---|
| 默认模式 | 0 | 打印原因,延迟1秒后调用 exit(0) |
| 自定义模式 | 1 | 仅触发回调,不自动退出 |
函数
txnet_set_exit_mode
cpp
void txnet_set_exit_mode(int mode);设置收到退出指令时的处理模式。
| 参数 | 类型 | 说明 |
|---|---|---|
mode | int | 0=默认模式,1=自定义模式 |
txnet_set_exit_callback
cpp
void txnet_set_exit_callback(txnet_exit_callback cb);设置退出指令回调函数。
回调函数
txnet_exit_callback
cpp
typedef void (*txnet_exit_callback)(int type, const char* reason);| 参数 | 类型 | 说明 |
|---|---|---|
type | int | 退出类型 |
reason | const char* | 退出原因 |
示例
默认模式
cpp
#include "txnet.h"
void onExit(int type, const char* reason) {
if (type == TXNET_EXIT_KICK) {
printf("被踢出: %s\n", reason);
} else {
printf("服务器要求退出: %s\n", reason);
}
}
int main() {
txnet_init(&config);
txnet_set_exit_callback(onExit);
txnet_connect();
txnet_login();
while (txnet_is_logged_in()) {
// 如果收到踢出指令,程序会自动退出
}
return 0;
}自定义模式(UI 集成)
cpp
#include "txnet.h"
void onExit(int type, const char* reason) {
if (type == TXNET_EXIT_KICK) {
showDialog("账号已下线", reason, []() {
exit(0);
});
} else {
showDialog("服务器通知", reason, []() {
exit(0);
});
}
}
int main() {
txnet_init(&config);
txnet_set_exit_mode(1);
txnet_set_exit_callback(onExit);
txnet_connect();
txnet_login();
while (txnet_is_logged_in()) {
// 回调会被触发,但程序不会自动退出
}
return 0;
}