### 7.2 世界聊天系統實現
接下來,我們來做一個玩家和玩家之間的世界聊天廣播功能。
A\) proto3協議定義
這里涉及到了MsgId:2的指令,還有對應的Talk的proto協議。
`MsgID`:2
`Talk`:
* 同步玩家本次登錄的ID\(用來標識玩家\), 玩家登陸之后,由Server端主動生成玩家ID發送給客戶端
* 發起者: Client
* Content: 聊天信息
```protobuf
message Talk{
string Content=1;
}
```
所以我們應該先修改proto文件
> mmo\_game/pb/msg.proto
```protobuf
syntax="proto3"; //Proto協議
package pb; //當前包名
option csharp_namespace="Pb"; //給C#提供的選項
//同步客戶端玩家ID
message SyncPid{
int32 Pid=1;
}
//玩家位置
message Position{
float X=1;
float Y=2;
float Z=3;
float V=4;
}
//玩家廣播數據
message BroadCast{
int32 Pid=1;
int32 Tp=2; //1-世界聊天 2-玩家位置
oneof Data {
string Content=3; //聊天的信息
Position P=4; //廣播用戶的位置
int32 ActionData=5;
}
}
//=====================
//玩家聊天數據
message Talk{
string Content=1; //聊天內容
}
//=====================
```
執行build.sh 生成新的`msg.proto.go`文件。
#### B\) 聊天業務API建立
接下來,我們創建一個api文件
> mmo\_game/api/world\_chat.go
```go
package api
import (
"fmt"
"github.com/golang/protobuf/proto"
"zinx/ziface"
"zinx/zinx_app_demo/mmo_game/core"
"zinx/zinx_app_demo/mmo_game/pb"
"zinx/znet"
)
//世界聊天 路由業務
type WorldChatApi struct {
znet.BaseRouter
}
func (*WorldChatApi) Handle(request ziface.IRequest) {
//1. 將客戶端傳來的proto協議解碼
msg := &pb.Talk{}
err := proto.Unmarshal(request.GetData(), msg)
if err != nil {
fmt.Println("Talk Unmarshal error ", err)
return
}
//2. 得知當前的消息是從哪個玩家傳遞來的,從連接屬性pid中獲取
pid, err := request.GetConnection().GetProperty("pid")
if err != nil {
fmt.Println("GetProperty pid error", err)
request.GetConnection().Stop()
return
}
//3. 根據pid得到player對象
player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))
//4. 讓player對象發起聊天廣播請求
player.Talk(msg.Content)
}
```
這里實際上對于msgID:2的路由業務函數的實現。其中有個小細節需要注意一下。第2步,根據鏈接conn得到當前玩家的pid,應該是我們之前在玩家上線的時候,將pid和conn做一個屬性綁定,如下:
> mmo\_game/server.go
```go
//當客戶端建立連接的時候的hook函數
func OnConnecionAdd(conn ziface.IConnection) {
//創建一個玩家
player := core.NewPlayer(conn)
//同步當前的PlayerID給客戶端, 走MsgID:1 消息
player.SyncPid()
//同步當前玩家的初始化坐標信息給客戶端,走MsgID:200消息
player.BroadCastStartPosition()
//將當前新上線玩家添加到worldManager中
core.WorldMgrObj.AddPlayer(player)
//=================將該連接綁定屬性Pid===============
conn.SetProperty("pid", player.Pid)
//===============================================
fmt.Println("=====> Player pidId = ", player.Pid, " arrived ====")
}
```
接下來,我們來看一下Player里的Talk實現方法:
> mmo\_game/core/player.go
```go
//廣播玩家聊天
func (p *Player) Talk(content string) {
//1. 組建MsgId200 proto數據
msg := &pb.BroadCast{
Pid:p.Pid,
Tp:1,//TP 1 代表聊天廣播
Data: &pb.BroadCast_Content{
Content: content,
},
}
//2. 得到當前世界所有的在線玩家
players := WorldMgrObj.GetAllPlayers()
//3. 向所有的玩家發送MsgId:200消息
for _, player := range players {
player.SendMsg(200, msg)
}
}
```
#### C\) 測試世界聊天功能
我們在服務端運行server
```bash
$go run server.go
$ go run server.go
Add api msgId = 2
[START] Server name: Zinx Game,listenner at IP: 0.0.0.0, Port 8999 is starting
[Zinx] Version: V0.11, MaxConn: 3000, MaxPacketSize: 4096
start Zinx server Zinx Game succ, now listenning...
Worker ID = 9 is started.
Worker ID = 4 is started.
Worker ID = 5 is started.
Worker ID = 6 is started.
Worker ID = 7 is started.
Worker ID = 8 is started.
Worker ID = 0 is started.
Worker ID = 1 is started.
Worker ID = 2 is started.
Worker ID = 3 is started.
```
打開兩個客戶端,分別互相聊天,效果如下,我們的聊天功能已經實現了。

- 一、引言
- 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廣播(跨越格子)