Skip to content

Commit 29c0201

Browse files
committed
docs: 更新 README 以反映 Web ProtoBuff 组件变更
更新 README 文档以匹配最新的 Web ProtoBuff 组件功能,包括: - 修改组件名称和描述 - 更新功能概述和方法说明 - 添加环境要求和使用示例 - 更新安装方式
1 parent 1f990c7 commit 29c0201

1 file changed

Lines changed: 90 additions & 108 deletions

File tree

README.md

Lines changed: 90 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,138 @@
11
## HOMEPAGE
22

3-
GameFrameX 的Web 请求组件
3+
GameFrameX 的 Web ProtoBuff 请求组件
44

5-
**Web 请求 组件 (Web Request)** - 提供使用短连接的功能,可以用 Get 或者 Post 方法向服务器发送请求并获取响应数据,可指定允许几个 Web 请求器进行同时请求
5+
**Web ProtoBuff 组件 (Web ProtoBuff Component)** - 提供基于 Protocol Buffer 的 HTTP 网络请求功能,支持异步发送和接收 ProtoBuff 消息
66

7-
# 使用文档(文档编写于GPT4)
7+
# 使用文档
88

9-
# WebComponent 说明文档
10-
11-
`WebComponent` 类是一个游戏框架组件,用作处理网络请求的模块。它提供了一系列方法来发送 GET 和 POST 请求,并获取返回的字符串或字节数组数据。以下是该类的详细说明和使用方法。
9+
`WebProtoBuffComponent` 类是一个游戏框架组件,专门用于处理基于 Protocol Buffer 的网络请求。它提供了一系列方法来发送 POST 请求,并自动处理 ProtoBuf 消息的序列化与反序列化。
1210

1311
## 功能概述
1412

15-
- 初始化网络管理器
16-
- 发送 GET 请求并获取返回的字符串或字节数组
17-
- 发送 POST 请求并获取返回的字符串或字节数组
18-
19-
## 方法说明
20-
21-
### Awake
22-
23-
初始化游戏框架组件。创建 `WebManager` 实例并获取网络管理器模块。
24-
25-
```csharp
26-
protected override void Awake() { /* 方法体省略 */ }
27-
```
28-
29-
### GetToString(重载1)
30-
31-
发送 GET 请求并以字符串形式获取响应。
32-
33-
```csharp
34-
public Task<string> GetToString(string url) { /* 方法体省略 */ }
35-
```
13+
- **ProtoBuf 支持**: 原生支持 Protocol Buffer 消息格式。
14+
- **异步操作**: 基于 `Task<T>` 的异步 API,方便使用 `async/await`
15+
- **超时控制**: 支持自定义请求超时时间。
16+
- **跨平台**: 兼容 Unity WebGL 及其他原生平台。
3617

37-
### GetToString(重载2)
18+
## 环境要求
3819

39-
发送带参数的 GET 请求并以字符串形式获取响应。
40-
41-
```csharp
42-
public Task<string> GetToString(string url, Dictionary<string, string> queryString) { /* 方法体省略 */ }
43-
```
44-
45-
### GetToString(重载3)
46-
47-
发送带参数和请求头的 GET 请求并以字符串形式获取响应。
48-
49-
```csharp
50-
public Task<string> GetToString(string url, Dictionary<string, string> queryString, Dictionary<string, string> header) { /* 方法体省略 */ }
51-
```
52-
53-
### GetToBytes(重载1)
54-
55-
发送 GET 请求并以字节数组形式获取响应。
56-
57-
```csharp
58-
public Task<byte[]> GetToBytes(string url) { /* 方法体省略 */ }
59-
```
20+
使用本组件需要在 Unity 的 **Player Settings** -> **Scripting Define Symbols** 中添加以下宏定义:
6021

61-
### GetToBytes(重载2)
22+
`ENABLE_GAME_FRAME_X_WEB_PROTOBUF_NETWORK`
6223

63-
发送带参数的 GET 请求并以字节数组形式获取响应。
64-
65-
```csharp
66-
public Task<byte[]> GetToBytes(string url, Dictionary<string, string> queryString) { /* 方法体省略 */ }
67-
```
24+
## 方法说明
6825

69-
### GetToBytes(重载3)
26+
### Awake
7027

71-
发送带参数和请求头的 GET 请求并以字节数组形式获取响应
28+
初始化游戏框架组件。获取 `IWebProtoBuffManager` 模块并配置超时时间
7229

7330
```csharp
74-
public Task<byte[]> GetToBytes(string url, Dictionary<string, string> queryString, Dictionary<string, string> header) { /* 方法体省略 */ }
31+
protected override void Awake() { /* ... */ }
7532
```
7633

77-
### PostToString(重载1)
34+
### Post<T>
7835

79-
发送 POST 请求并以字符串形式获取响应
36+
发送 ProtoBuf 消息并等待响应
8037

8138
```csharp
82-
public Task<string> PostToString(string url, Dictionary<string, string> from = null) { /* 方法体省略 */ }
39+
public Task<T> Post<T>(string url, GameFrameX.Network.Runtime.MessageObject message)
40+
where T : GameFrameX.Network.Runtime.MessageObject, GameFrameX.Network.Runtime.IResponseMessage
8341
```
8442

85-
### PostToString(重载2)
43+
- **参数**:
44+
- `url`: 目标服务器的 URL 地址。
45+
- `message`: 要发送的消息对象(必须继承自 `MessageObject`)。
46+
- **返回值**:
47+
- 返回一个 `Task<T>`,任务完成后包含服务器响应的消息对象。
48+
- **类型参数**:
49+
- `T`: 响应消息的类型(必须继承自 `MessageObject` 并实现 `IResponseMessage`)。
8650

87-
发送带表单和 URL 请求参数的 POST 请求并以字符串形式获取响应。
51+
### Timeout
8852

89-
```csharp
90-
public Task<string> PostToString(string url, Dictionary<string, string> from, Dictionary<string, string> queryString) { /* 方法体省略 */ }
91-
```
92-
93-
### PostToString(重载3)
94-
95-
发送带表单、URL 请求参数和请求头的 POST 请求并以字符串形式获取响应。
53+
获取或设置请求超时时间(单位:秒)。
9654

9755
```csharp
98-
public Task<string> PostToString(string url, Dictionary<string, string> from, Dictionary<string, string> queryString, Dictionary<string, string> header) { /* 方法体省略 */ }
56+
public float Timeout { get; set; }
9957
```
10058

101-
### PostToBytes(重载1)
102-
103-
发送 POST 请求并以字节数组形式获取响应。
104-
105-
```csharp
106-
public Task<byte[]> PostToBytes(string url, Dictionary<string, string> from) { /* 方法体省略 */ }
107-
```
59+
## 使用示例
10860

109-
### PostToBytes(重载2)
61+
### 1. 定义消息
11062

111-
发送带表单和 URL 请求参数的 POST 请求并以字节数组形式获取响应
63+
首先定义请求和响应的 ProtoBuf 消息类
11264

11365
```csharp
114-
public Task<byte[]> PostToBytes(string url, Dictionary<string, string> from, Dictionary<string, string> queryString) { /* 方法体省略 */ }
115-
```
116-
117-
### PostToBytes(重载3)
118-
119-
发送带表单、URL 请求参数和请求头的 POST 请求并以字节数组形式获取响应。
66+
using ProtoBuf;
67+
using GameFrameX.Network.Runtime;
12068

121-
```csharp
122-
public Task<byte[]> PostToBytes(string url, Dictionary<string, string> from, Dictionary<string, string> queryString, Dictionary<string, string> header) { /* 方法体省略 */ }
69+
[ProtoContract]
70+
public class LoginRequest : MessageObject
71+
{
72+
[ProtoMember(1)]
73+
public string Username { get; set; }
74+
75+
[ProtoMember(2)]
76+
public string Password { get; set; }
77+
}
78+
79+
[ProtoContract]
80+
public class LoginResponse : MessageObject, IResponseMessage
81+
{
82+
[ProtoMember(1)]
83+
public bool Success { get; set; }
84+
85+
[ProtoMember(2)]
86+
public string Token { get; set; }
87+
}
12388
```
12489

125-
## 使用示例
90+
### 2. 发送请求
12691

127-
1. 调用 `GetToString` 方法获取不带参数的 GET 请求响应字符串:
92+
在组件中使用 `WebProtoBuffComponent` 发送请求。
12893

12994
```csharp
130-
Task<string> response = webComponent.GetToString("http://example.com/api/values");
131-
```
132-
133-
2. 使用 `PostToBytes` 方法发送带表单参数的 POST 请求,并以字节数组接收响应:
95+
using UnityEngine;
96+
using GameFrameX.Web.ProtoBuff.Runtime;
13497

135-
```csharp
136-
Dictionary<string, string> formData = new Dictionary<string, string>
98+
public class LoginController : MonoBehaviour
13799
{
138-
{ "param1", "value1" },
139-
{ "param2", "value2" }
140-
};
141-
Task<byte[]> responseBytes = webComponent.PostToBytes("http://example.com/api/upload", formData);
100+
public WebProtoBuffComponent WebComponent;
101+
102+
private async void Start()
103+
{
104+
var request = new LoginRequest
105+
{
106+
Username = "user",
107+
Password = "password"
108+
};
109+
110+
string url = "http://api.example.com/login";
111+
112+
try
113+
{
114+
// 发送请求并等待结果
115+
LoginResponse response = await WebComponent.Post<LoginResponse>(url, request);
116+
117+
if (response != null)
118+
{
119+
Debug.Log($"Login Result: {response.Success}, Token: {response.Token}");
120+
}
121+
}
122+
catch (System.Exception e)
123+
{
124+
Debug.LogError($"Request Failed: {e.Message}");
125+
}
126+
}
127+
}
142128
```
143129

144-
## 注意事项
145-
146-
确保在网络请求期间合适地处理任务,例如使用 `await` 异步等待结果。
147-
148-
# 使用方式(任选其一)
130+
## 安装方式(任选其一)
149131

150132
1. 直接在 `manifest.json` 的文件中的 `dependencies` 节点下添加以下内容
151133
```json
152-
{"com.gameframex.unity.web": "https://github.com/AlianBlank/com.gameframex.unity.web.git"}
134+
{"com.gameframex.unity.web.protobuff": "https://github.com/AlianBlank/com.gameframex.unity.web.protobuff.git"}
153135
```
154-
2. 在Unity 的`Packages Manager` 中使用`Git URL` 的方式添加库,地址为:https://github.com/AlianBlank/com.gameframex.unity.web.git
136+
2. 在 Unity 的 `Packages Manager` 中使用 `Git URL` 的方式添加库,地址为:`https://github.com/AlianBlank/com.gameframex.unity.web.protobuff.git`
155137

156-
3. 直接下载仓库放置到Unity 项目的`Packages` 目录下。会自动加载识别
138+
3. 直接下载仓库放置到 Unity 项目的 `Packages` 目录下。会自动加载识别

0 commit comments

Comments
 (0)