Files
RedHotRoast-ios/Assets/SGModule/Net/README.md
T

67 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🌐 Net 网络模块
适用于 Unity 的网络模块,包含网络检测、接口请求、Token 鉴权、服务器时间同步等功能。模块职责清晰、易扩展,使用前需引入通用模块。
------
## 📦 模块概览
| 模块 | 功能描述 |
| ---------------- | --------------------------------------------------- |
| `NetChecker` | 📶 检测网络是否可用,支持 Ping / HTTP 检查 |
| `NetCore<T>` | 📡 网络请求基类,支持加密和 Token 自动处理(需继承) |
| `TokenManager` | 🔐 Token 缓存 / 自动刷新 / 登录回退处理 |
| `ServerClock` | 🕒 同步服务器时间,避免设备本地误差 |
| `WebSocket 模块` | 依赖 nativewebsocket 插件 |
------
## ✅ 使用示例
### 1. 网络检测
```c#
StartCoroutine(NetChecker.Instance.WaitUntilNetworkConnected(10, 1f, success => {
if (success) Debug.Log("✅ 网络可用");
}));
```
------
### 2. 实现你的请求类(必须继承 NetCore)
```c#
public class NetKit : NetCore<NetKit> {
// 详情参考此类
}
```
------
### 3. 发起接口请求(在子类中)
```c#
NetKit.Instance.Post<TrackData>("/event/incrN", trackData,
response => {
callback?.Invoke(response.IsSuccess, response.Data);
});
```
------
### 4. Token 获取(自动缓存)
```c#
yield return TokenManager.Instance.GetToken(token => {
Debug.Log("当前 Token" + token);
});
```
------
### 5. 服务器时间同步
```c#
ServerClock.Init(serverUnixTime);
var now = ServerClock.GetCurrentServerTime();
```