You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
964 B

4 years ago
4 years ago
4 years ago
4 years ago
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/aceld/zinx/ziface"
  5. "github.com/aceld/zinx/zinx_app_demo/mmo_game/core"
  6. "github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
  7. "github.com/aceld/zinx/znet"
  8. "github.com/golang/protobuf/proto"
  9. )
  10. //世界聊天 路由业务
  11. type WorldChatApi struct {
  12. znet.BaseRouter
  13. }
  14. func (*WorldChatApi) Handle(request ziface.IRequest) {
  15. //1. 将客户端传来的proto协议解码
  16. msg := &pb.Talk{}
  17. err := proto.Unmarshal(request.GetData(), msg)
  18. if err != nil {
  19. fmt.Println("Talk Unmarshal error ", err)
  20. return
  21. }
  22. //2. 得知当前的消息是从哪个玩家传递来的,从连接属性pID中获取
  23. pID, err := request.GetConnection().GetProperty("pID")
  24. if err != nil {
  25. fmt.Println("GetProperty pID error", err)
  26. request.GetConnection().Stop()
  27. return
  28. }
  29. //3. 根据pID得到player对象
  30. player := core.WorldMgrObj.GetPlayerByPID(pID.(int32))
  31. //4. 让player对象发起聊天广播请求
  32. player.Talk(msg.Content)
  33. }