|
|
# Examples
|
|
|
|
|
|
本目录包含 nnet 网络库的各种使用示例。
|
|
|
|
|
|
## 运行示例
|
|
|
|
|
|
每个示例目录包含 `server.go` 和 `client.go` 两个文件,使用 Go 构建标签区分:
|
|
|
|
|
|
### 运行服务器
|
|
|
|
|
|
```bash
|
|
|
# 默认运行 server(不指定标签)
|
|
|
go run ./examples/client_pool
|
|
|
|
|
|
# 或者明确指定 server 标签
|
|
|
go run -tags server ./examples/client_pool
|
|
|
|
|
|
# 编译服务器
|
|
|
go build -tags server -o server.exe ./examples/client_pool
|
|
|
```
|
|
|
|
|
|
### 运行客户端
|
|
|
|
|
|
```bash
|
|
|
# 必须指定 client 标签
|
|
|
go run -tags client ./examples/client_pool
|
|
|
|
|
|
# 编译客户端
|
|
|
go build -tags client -o client.exe ./examples/client_pool
|
|
|
```
|
|
|
|
|
|
## 示例列表
|
|
|
|
|
|
### client_pool
|
|
|
|
|
|
演示客户端连接池的使用。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/client_pool
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/client_pool
|
|
|
```
|
|
|
|
|
|
### interceptor
|
|
|
|
|
|
演示拦截器链的使用,包括数据验证、转换和过滤。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/interceptor
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
# 验证数据
|
|
|
go run -tags client ./examples/interceptor -addr tcp://127.0.0.1:8084 -cmd validate -data "hello"
|
|
|
|
|
|
# 测试短数据(会被拒绝)
|
|
|
go run -tags client ./examples/interceptor -addr tcp://127.0.0.1:8084 -cmd validate -data "hi"
|
|
|
|
|
|
# 测试禁止的数据
|
|
|
go run -tags client ./examples/interceptor -addr tcp://127.0.0.1:8084 -cmd validate -data "forbidden"
|
|
|
|
|
|
# Echo 测试
|
|
|
go run -tags client ./examples/interceptor -addr tcp://127.0.0.1:8084 -cmd echo -data "test"
|
|
|
```
|
|
|
|
|
|
### middleware
|
|
|
|
|
|
演示中间件的使用。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/middleware
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/middleware -addr tcp://127.0.0.1:8085
|
|
|
```
|
|
|
|
|
|
### protocol_version
|
|
|
|
|
|
演示协议版本管理。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/protocol_version
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/protocol_version -addr tcp://127.0.0.1:8082 -cmd version
|
|
|
go run -tags client ./examples/protocol_version -addr tcp://127.0.0.1:8082 -cmd echo -data "hello"
|
|
|
```
|
|
|
|
|
|
### session_file
|
|
|
|
|
|
演示基于文件的会话存储。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/session_file
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/session_file -addr tcp://127.0.0.1:8083 -cmd set
|
|
|
go run -tags client ./examples/session_file -addr tcp://127.0.0.1:8083 -cmd get
|
|
|
go run -tags client ./examples/session_file -addr tcp://127.0.0.1:8083 -cmd counter
|
|
|
```
|
|
|
|
|
|
### udp_echo
|
|
|
|
|
|
演示 UDP 协议的 echo 服务器和客户端。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/udp_echo
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/udp_echo -addr udp://127.0.0.1:8084 -data "hello-udp"
|
|
|
```
|
|
|
|
|
|
### ws_echo
|
|
|
|
|
|
演示 WebSocket 协议的 echo 服务器和客户端。
|
|
|
|
|
|
**服务器**:
|
|
|
```bash
|
|
|
go run -tags server ./examples/ws_echo
|
|
|
```
|
|
|
|
|
|
**客户端**:
|
|
|
```bash
|
|
|
go run -tags client ./examples/ws_echo -addr ws://127.0.0.1:8083 -data "hello-ws"
|
|
|
```
|
|
|
|
|
|
## 其他示例
|
|
|
|
|
|
### basic
|
|
|
|
|
|
基本的服务器和客户端示例。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/basic
|
|
|
```
|
|
|
|
|
|
### router_frame_data
|
|
|
|
|
|
演示基于帧数据的路由匹配。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/router_frame_data
|
|
|
```
|
|
|
|
|
|
### metrics_health
|
|
|
|
|
|
演示指标和健康检查功能。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/metrics_health
|
|
|
```
|
|
|
|
|
|
### preset
|
|
|
|
|
|
演示 `PresetBuilder` 的快速启动方式,并自动挂载 metrics/health HTTP 端点。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/preset
|
|
|
```
|
|
|
|
|
|
### client_request
|
|
|
|
|
|
演示客户端请求-响应模式。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/client_request
|
|
|
```
|
|
|
|
|
|
### write_any
|
|
|
|
|
|
演示多种写入方式。
|
|
|
|
|
|
```bash
|
|
|
go run ./examples/write_any
|
|
|
```
|
|
|
|
|
|
## 构建标签说明
|
|
|
|
|
|
- `server`: 编译服务器代码
|
|
|
- `client`: 编译客户端代码
|
|
|
- 默认(无标签): 编译服务器代码
|
|
|
|
|
|
使用构建标签可以:
|
|
|
1. 在同一个目录中组织 server 和 client 代码
|
|
|
2. 分别编译和运行 server 和 client
|
|
|
3. 保持代码组织清晰,避免目录过多
|
|
|
|
|
|
## 注意事项
|
|
|
|
|
|
1. 运行客户端前,请确保服务器已经启动
|
|
|
2. 某些示例需要特定的命令行参数,请参考各个示例的注释
|
|
|
3. 端口冲突时,可以修改服务器配置中的端口号
|
|
|
4. WebSocket 和 UDP 示例需要使用对应的协议地址(`ws://` 或 `udp://`)
|
|
|
|