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.

66 lines
1.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aceld/zinx/ziface"
  5. "github.com/aceld/zinx/zinx_app_demo/mmo_game/api"
  6. "github.com/aceld/zinx/zinx_app_demo/mmo_game/core"
  7. "github.com/aceld/zinx/znet"
  8. )
  9. //当客户端建立连接的时候的hook函数
  10. func OnConnecionAdd(conn ziface.IConnection) {
  11. //创建一个玩家
  12. player := core.NewPlayer(conn)
  13. //同步当前的PlayerID给客户端, 走MsgID:1 消息
  14. player.SyncPID()
  15. //同步当前玩家的初始化坐标信息给客户端,走MsgID:200消息
  16. player.BroadCastStartPosition()
  17. //将当前新上线玩家添加到worldManager中
  18. core.WorldMgrObj.AddPlayer(player)
  19. //将该连接绑定属性PID
  20. conn.SetProperty("pID", player.PID)
  21. //同步周边玩家上线信息,与现实周边玩家信息
  22. player.SyncSurrounding()
  23. fmt.Println("=====> Player pIDID = ", player.PID, " arrived ====")
  24. }
  25. //当客户端断开连接的时候的hook函数
  26. func OnConnectionLost(conn ziface.IConnection) {
  27. //获取当前连接的PID属性
  28. pID, _ := conn.GetProperty("pID")
  29. //根据pID获取对应的玩家对象
  30. player := core.WorldMgrObj.GetPlayerByPID(pID.(int32))
  31. //触发玩家下线业务
  32. if pID != nil {
  33. player.LostConnection()
  34. }
  35. fmt.Println("====> Player ", pID, " left =====")
  36. }
  37. func main() {
  38. //创建服务器句柄
  39. s := znet.NewServer()
  40. //注册客户端连接建立和丢失函数
  41. s.SetOnConnStart(OnConnecionAdd)
  42. s.SetOnConnStop(OnConnectionLost)
  43. //注册路由
  44. s.AddRouter(2, &api.WorldChatApi{})
  45. s.AddRouter(3, &api.MoveApi{})
  46. //启动服务
  47. s.Serve()
  48. }