#### 10.3 鏈接屬性Zinx-V0.10單元測試
那么,接下來,我們簡單測試一下鏈接屬性的設置與提取是否可用。
> Server.go
```go
package main
import (
"fmt"
"zinx/ziface"
"zinx/znet"
)
//ping test 自定義路由
type PingRouter struct {
znet.BaseRouter
}
//Ping Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call PingRouter Handle")
//先讀取客戶端的數據,再回寫ping...ping...ping
fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
if err != nil {
fmt.Println(err)
}
}
type HelloZinxRouter struct {
znet.BaseRouter
}
//HelloZinxRouter Handle
func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
fmt.Println("Call HelloZinxRouter Handle")
//先讀取客戶端的數據,再回寫ping...ping...ping
fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.10"))
if err != nil {
fmt.Println(err)
}
}
//創建連接的時候執行
func DoConnectionBegin(conn ziface.IConnection) {
fmt.Println("DoConnecionBegin is Called ... ")
//=============設置兩個鏈接屬性,在連接創建之后===========
fmt.Println("Set conn Name, Home done!")
conn.SetProperty("Name", "Aceld")
conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
//===================================================
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
fmt.Println(err)
}
}
//連接斷開的時候執行
func DoConnectionLost(conn ziface.IConnection) {
//============在連接銷毀之前,查詢conn的Name,Home屬性=====
if name, err:= conn.GetProperty("Name"); err == nil {
fmt.Println("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
fmt.Println("Conn Property Home = ", home)
}
//===================================================
fmt.Println("DoConneciotnLost is Called ... ")
}
func main() {
//創建一個server句柄
s := znet.NewServer()
//注冊鏈接hook回調函數
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnStop(DoConnectionLost)
//配置路由
s.AddRouter(0, &PingRouter{})
s.AddRouter(1, &HelloZinxRouter{})
//開啟服務
s.Serve()
}
```
這里主要看`DoConnectionBegin()`和`DoConnectionLost()`兩個函數的實現, 利用在兩個Hook函數中,設置鏈接屬性和提取鏈接屬性。鏈接創建之后給當前鏈接綁定兩個屬性"Name","Home", 那么我們在隨時可以通過`conn.GetProperty()`方法得到鏈接已經設置的屬性。
```bash
$go run Server.go
```
```
$go run Client0.go
```
服務端:
```bash
$ go run Server.go
Add api msgId = 0
Add api msgId = 1
[START] Server name: zinx v-0.10 demoApp,listenner at IP: 127.0.0.1, Port 7777 is starting
[Zinx] Version: V0.4, MaxConn: 3, MaxPacketSize: 4096
start Zinx server zinx v-0.10 demoApp succ, now listenning...
Worker ID = 9 is started.
Worker ID = 5 is started.
Worker ID = 6 is started.
Worker ID = 7 is started.
Worker ID = 8 is started.
Worker ID = 1 is started.
Worker ID = 0 is started.
Worker ID = 2 is started.
Worker ID = 3 is started.
Worker ID = 4 is started.
connection add to ConnManager successfully: conn num = 1
---> CallOnConnStart....
DoConnecionBegin is Called ...
Set conn Name, Home done!
[Writer Goroutine is running]
[Reader Goroutine is running]
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
read msg head error read tcp4 127.0.0.1:7777->127.0.0.1:55208: read: connection reset by peer
Conn Stop()...ConnID = 0
---> CallOnConnStop....
Conn Property Name = Aceld
Conn Property Home = https://www.jianshu.com/u/35261429b7f1
DoConneciotnLost is Called ...
connection Remove ConnID= 0 successfully: conn num = 0
127.0.0.1:55208 [conn Reader exit!]
127.0.0.1:55208 [conn Writer exit!]
```
客戶端:
```bash
$ go run Client0.go
Client Test ... start
==> Recv Msg: ID= 2 , len= 21 , data= DoConnection BEGIN...
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
^Csignal: interrupt
```
當我們終止客戶端鏈接,那么服務端在斷開鏈接之前,已經讀取到了conn的兩個屬性Name和Home
- 一、引言
- 1、寫在前面
- 2、初探Zinx架構
- 二、初識Zinx框架
- 1. Zinx-V0.1-基礎Server
- 2.Zinx-V0.2-簡單的連接封裝與業務綁定
- 三、Zinx框架基礎路由模塊
- 3.1 IRequest 消息請求抽象類
- 3.2 IRouter 路由配置抽象類
- 3.3 Zinx-V0.3-集成簡單路由功能
- 3.4 Zinx-V0.3代碼實現
- 3.5 使用Zinx-V0.3完成應用程序
- 四、Zinx的全局配置
- 4.1 Zinx-V0.4增添全局配置代碼實現
- 4.2 使用Zinx-V0.4完成應用程序
- 五、Zinx的消息封裝
- 5.1 創建消息封裝類型
- 5.2 消息的封包與拆包
- 5.3 Zinx-V0.5代碼實現
- 5.4 使用Zinx-V0.5完成應用程序
- 六、Zinx的多路由模式
- 6.1 創建消息管理模塊
- 6.2 Zinx-V0.6代碼實現
- 6.3 使用Zinx-V0.6完成應用程序
- 七、Zinx的讀寫分離模型
- 7.1 Zinx-V0.7代碼實現
- 7.2 使用Zinx-V0.7完成應用程序
- 八、Zinx的消息隊列及多任務機制
- 8.1 創建消息隊列
- 8.2 創建及啟動Worker工作池
- 8.3 發送消息給消息隊列
- 8.4 Zinx-V0.8代碼實現
- 8.5 使用Zinx-V0.8完成應用程序
- 九、Zinx的鏈接管理
- 9.1 創建鏈接管理模塊
- 9.2 鏈接管理模塊集成到Zinx中
- 9.3 鏈接的帶緩沖的發包方法
- 9.4 注冊鏈接啟動/停止自定義Hook方法功能
- 9.5 使用Zinx-V0.9完成應用程序
- 十、Zinx的連接屬性設置
- 10.1 給鏈接添加鏈接配置接口
- 10.2 鏈接屬性方法實現
- 10.3 鏈接屬性Zinx-V0.10單元測試
- 基于Zinx的應用案例
- 一、應用案例介紹
- 二、服務器應用基礎協議
- 三、MMO多人在線游戲AOI算法
- 3.1 網絡法實現AOI算法
- 3.2 實現AOI格子結構
- 3.3 實現AOI管理模塊
- 3.4 求出九宮格
- 3.5 AOI格子添加刪除操作
- 3.6 AOI模塊單元測試
- 四、數據傳輸協議protocol buffer
- 4.1 簡介
- 4.2 數據交換格式
- 4.3 protobuf環境安裝
- 4.4 protobuf語法
- 4.5 編譯protobuf
- 4.6 利用protobuf生成的類來編碼
- 五、MMO游戲的Proto3協議
- 六、構建項目與用戶上線
- 6.1 構建項目
- 6.2用戶上線流程
- 七、世界聊天系統實現
- 7.1 世界管理模塊
- 7.2 世界聊天系統實現
- 八、上線位置信息同步
- 九、移動位置與AOI廣播(未跨越格子)
- 十、玩家下線
- 十一、移動與AOI廣播(跨越格子)