Skip to content

danie1Lin/Distributed-Golang-Game-Server

Repository files navigation

Golang Distributed Game Server

CircleCI

Motivation

At first, I just want to learn Golang.I started to think about which is the best way? Because the concurrency mechanisms of Golang is very powerful, I choose online game to verify if I can use Golang to make a efficient game server.For me, this is the first time I make this such hard project. I have to learn Unity, Golang, C# At a time. I am glad that I still have full passion to this project and I never give up.

Tech/framework used

  • golang
  • gRPC
  • Kubernetes
  • Protobuf

Features

  • CrossPlatform - Message packet use protobuf which is light, fast, crossplatform.
  • Autoscaling - controller is written with go-client ,you can wirte the strategy to autoscale dedicated game server by your own.
  • Lightweight - The image of dedicated game server is less than 40MB.

Architecture

  • Agent server :
    • match players to join other player's room or create own room
    • control the amount of gameplay server and load balancing. when the amountof a gameplay server's connections exceed maxium connections it should have, agent will create a new pod run gameplay server.
  • Gameplay Server :
    • After players are matched successfully ,these players will get the gameplay server's ip and token,and player can start to play.

Installation

  • Server : Two Way to run server:
    • Run distributed server using Kubernete cluster
      1. Use Kops to install kubernete on AWS
      2. Create cluster
      3. Install Mongodb && and edit setupEnv.sh with your setting and bash setupEnv.sh
      4. go run main.go --type=agent on your local machine (Must on Where you install Kops)
    • Run Standalone Server on local machine
      1. Install Mongodb && and edit setupEnv.sh( DONT_USE_KUBE = true )with your setting and bash setupEnv.sh
      2. go run main.go --type=standalone on your local machine
  • Client :
    • You can run in Unity Editor by open the Prestart.scene as first scene.
    • If you want to test with mutliplayer you can try build Andorid apk because it is likely to be builded successfully.

How to use?

If you want to make your own game by modifying this project, I am pleasured. You can throught these step to make it work.

Modify The msg/message.proto

  1. Change the GameFrame message in proto buff.
  2. Run './update.sh' in 'msg/'
  3. unzip message.zip under 'Asset/gameServer/proto' in the Unity Project.

Create Your Game Logic

  1. add your handler to "gameServer/game/session/room.go": func (r *Room) Run()
  2. modify the UpdateFrame fuction to handle packets design by yourself. then,Data Flow to Entity to render the change of entity's properties.

The file structure:

  1. agent
    1. session
      1. room
      2. session
      3. kubernete
  2. game
    1. session
      1. room
      2. session
  3. msg
    - use protobuf to define package and rpc interface
  4. uuid
    generate different IDs of objects that can be call with reflection
  5. user
    • UserManager
    • User
    • TODO: There should be a session manager to cache user infomation and state
    • storage
      Use mogdb to storage user infomation

Contribute

Credits

License

MIT © daniel840829

Releases

No releases published

Packages

No packages published

Languages