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.

42 lines
1.0 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "zinx/ziface"
  5. "zinx/zinx_app_demo/mmo_game/api"
  6. "zinx/zinx_app_demo/mmo_game/core"
  7. "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. func main() {
  26. //创建服务器句柄
  27. s := znet.NewServer()
  28. //注册客户端连接建立和丢失函数
  29. s.SetOnConnStart(OnConnecionAdd)
  30. //注册路由
  31. s.AddRouter(2, &api.WorldChatApi{})
  32. s.AddRouter(3, &api.MoveApi{})
  33. //启动服务
  34. s.Serve()
  35. }