package module
v0.1.6 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2018 License: Apache-2.0 Imports: 11 Imported by: 0



Scalable Distributed Game Server Engine with Hot Reload in Golang

GoDoc Build Status Go Report Card codecov ApacheLicense

中文资料: 游戏服务器介绍 目录结构说明 使用GoWorld轻松实现分布式聊天服务器 欢迎加入Go服务端开发交流群:662182346


  • Spaces & Entities: manage multiple spaces and entities with AOI support
  • Distributed: increase server capacity by using more machines
  • Hot-Swappable: update game logic by restarting server process
  • Multiple Communication Protocols: supports TCP, KCP and WebSocket
  • Traffic Compression & Encryption: traffic between clients and servers can be compressed and encrypted


GoWorld Architecture


GoWorld server adopts an entity framework, in which entities represent all players, monsters, NPCs. Entities in the same space can visit each other directly by calling methods or access attributes. Entities in different spaces can call each over using RPC.

A GoWorld server consists of one dispatcher, one or more games and one or more gates. The gates are responsible for handling client connections and receive/send packets from/to clients. The games manages all entities and runs all game logic. The dispatcher is responsible for redirecting packets among games and between games and gates.

The game processes are hot-swappable. We can swap a game by sending SIGHUP to the process and restart the process with -restore parameter to bring game back to work but with the latest executable image. This feature enables updating server-side logic or fixing server bugs transparently without significant interference of online players.

Installing GoWorld

GoWorld requries Go 1.10.x to install.

go get -d github.com/xiaonanln/goworld
go get -u github.com/golang/dep/cmd/dep
cd $GOPATH/src/github.com/xiaonanln/goworld
dep ensure
go get ./cmd/...

GoWorld uses Dep to manage packages.

For Chinese only: 中国的朋友可能会碰到网络问题,那么您可以通过点击下载vendor.tar.gz,并将其解压到goworld根目录下。

Or using Docker:

# debian-jessie + golang + goworld
docker run -it xiaonanln/goworld
# debian-jessie + golang + goworld + mongodb 3.4 
docker run -it xiaonanln/goworld-dev

Manage GoWorld Servers

Use command goworld to build, start, stop and reload game servers.

Build Example Chatroom Server:

$ goworld build examples/chatroom_demo

Start Example Chatroom Server: (dispatcher -> game -> gate)

$ goworld start examples/chatroom_demo

Stop Game Server (gate -> game -> dispatcher):

$ goworld stop examples/chatroom_demo

Reload Game Servers:

$ goworld reload examples/chatroom_demo

Reload will reboot game processes with the current executable while preserving all game server states. However, it does not work on Windows.

List Server Processes:

$ goworld status examples/chatroom_demo
> 1 dispatcher running, 1/1 gates running, 1/1 games (examples/chatroom_demo) running
> 	2763      dispatcher      /home/ubuntu/go/src/github.com/xiaonanln/goworld/components/dispatcher/dispatcher -dispid 1
> 	2770      chatroom_demo   /home/ubuntu/go/src/github.com/xiaonanln/goworld/examples/chatroom_demo/chatroom_demo -gid 1
> 	2779      gate            /home/ubuntu/go/src/github.com/xiaonanln/goworld/components/gate/gate -gid 1


Chatroom Demo

The chatroom demo is a simple implementation of chatroom server and client. It illustrates how GoWorld can also be used for games which don't divide players by spaces.

The chatroom demo provides following features:

  • register & login
  • send chat message
  • switch chatrooms

Build Server:

goworld build examples/chatroom_demo

Run Server:

goworld start examples/chatroom_demo

Chatroom Demo Client:

Chatroom demo client implements the client-server protocol in Javascript.
The client for chatroom demo is hosted at github.com/xiaonanln/goworld-chatroom-demo-client. The project was created and built in Cocos Creater 1.5. A running server and client demo can also be found at http://goworldgs.com/chatclient/.

Unity Demo

Unity Demo is a simple multiple player monster shooting game to show how spaces and entities of GoWorld can be used to create multiple player online games.

  • register & login
  • move players in space
  • summon monsters
  • player shoot monsters
  • monsters attack players

Developing features:

  • Hit effects
  • Players migrate among multiple spaces
  • Server side map navigation

Build Server:

goworld build examples/unity_demo

Run Server:

goworld start examples/unity_demo

Unity Demo Client:

Unity demo client implements the client-server protocol in C#. The client for unity demo is hosted at https://github.com/xiaonanln/goworld-unity-demo. The project was created and built in Unity 2017.1.

You can try the demo by downloading GoWorldUnityDemo.zip. The demo connects to a goworld server on Amazon EC2 instance.




This section is empty.


This section is empty.


func AddCallback added in v0.1.4

func AddCallback(d time.Duration, callback func())

AddTimer adds a timer to be executed after specified duration

func AddTimer added in v0.1.4

func AddTimer(d time.Duration, callback func())

AddTimer adds a repeat timer to be executed every specified duration

func Call added in v0.1.3

func Call(id EntityID, method string, args ...interface{})

Call other entities

func CallNilSpaces added in v0.1.3

func CallNilSpaces(method string, args ...interface{})

CallNilSpaces calls methods of all nil spaces on all games

func CallService added in v0.1.3

func CallService(serviceName string, method string, args ...interface{})

CallService calls a service entity

func Entities

func Entities() entity.EntityMap

Entities gets all entities as an EntityMap (do not modify it!)

func Exists

func Exists(typeName string, entityID EntityID, callback storage.ExistsCallbackFunc)

Exists checks if entityID exists in entity storage

returns result in callback

func GetGameID

func GetGameID() uint16

GetGameID gets the local server ID

server ID is a uint16 number starts from 1, which should be different for each servers server ID is also in the game config section name of goworld.ini

func GetKVDB

func GetKVDB(key string, callback kvdb.KVDBGetCallback)

GetKVDB gets value of key from KVDB

func GetOrPutKVDB

func GetOrPutKVDB(key string, val string, callback kvdb.KVDBGetOrPutCallback)

GetOrPut gets value of key from KVDB, if val not exists or is "", put key-value to KVDB.

func GetServiceEntityID added in v0.1.4

func GetServiceEntityID(serviceName string) common.EntityID

GetServiceEntityID returns the entityid of the service

func ListAttr

func ListAttr() *entity.ListAttr

ListAttr creates a new ListAttr

func ListEntityIDs

func ListEntityIDs(typeName string, callback storage.ListCallbackFunc)

ListEntityIDs gets all saved entity ids in storage, may take long time and block the main routine

returns result in callback

func ListGameIDs added in v0.1.4

func ListGameIDs() []uint16

ListGameIDs returns all game IDs

func LoadEntityAnywhere

func LoadEntityAnywhere(typeName string, entityID EntityID)

LoadEntityAnywhere loads the specified entity from entity storage

func LoadEntityLocally added in v0.1.6

func LoadEntityLocally(typeName string, entityID EntityID)

LoadEntityLocally load entity in the local game If the entity already exists on any server, LoadEntityLocally will do nothing

func LoadEntityOnGame added in v0.1.6

func LoadEntityOnGame(typeName string, entityID EntityID, gameid uint16)

LoadEntityOnGame loads entity in the specified game If the entity already exists on any server, LoadEntityOnGame will do nothing

func MapAttr

func MapAttr() *entity.MapAttr

MapAttr creates a new MapAttr

func Post

func Post(callback post.PostCallback)

Post posts a callback to be executed It is almost same as AddCallback(0, callback)

func PutKVDB

func PutKVDB(key string, val string, callback kvdb.KVDBPutCallback)

PutKVDB puts key-value to KVDB

func RegisterCrontab added in v0.1.6

func RegisterCrontab(minute, hour, day, month, dayofweek int, cb func())

RegisterCrontab a callack which will be executed when time condition is satisfied

param minute: time condition satisfied on the specified minute, or every -minute if minute is negative param hour: time condition satisfied on the specified hour, or every -hour when hour is negative param day: time condition satisfied on the specified day, or every -day when day is negative param month: time condition satisfied on the specified month, or every -month when month is negative param dayofweek: time condition satisfied on the specified week day, or every -dayofweek when dayofweek is negative param cb: callback function to be executed when time is satisfied

func RegisterEntity

func RegisterEntity(typeName string, entityPtr entity.IEntity) *entity.EntityTypeDesc

RegisterEntity registers the entity type so that entities can be created or loaded

returns the entity type description object which can be used to define more properties of entity type

func RegisterService added in v0.1.4

func RegisterService(typeName string, entityPtr entity.IEntity)

RegisterService registeres an service type After registeration, the service entity will be created automatically on some game

func RegisterSpace

func RegisterSpace(spacePtr entity.ISpace)

RegisterSpace registers the space entity type.

All spaces will be created as an instance of this type

func Run

func Run()

Run runs the server endless loop

This is the main routine for the server and all entity logic, and this function never quit


type Entity added in v0.1.4

type Entity = entity.Entity

func CreateEntityLocally

func CreateEntityLocally(typeName string) *Entity

CreateEntityLocally creates a entity on the local server

returns EntityID

func GetEntity

func GetEntity(id EntityID) *Entity

GetEntity gets the entity by EntityID

type EntityID added in v0.1.4

type EntityID = common.EntityID

func CreateEntityAnywhere

func CreateEntityAnywhere(typeName string) EntityID

CreateEntitySomewhere creates a entity on any server

func CreateEntityOnGame added in v0.1.6

func CreateEntityOnGame(gameid uint16, typeName string) EntityID

func CreateSpaceAnywhere

func CreateSpaceAnywhere(kind int) EntityID

CreateSpaceAnywhere creates a space with specified kind in any game server

func CreateSpaceOnGame added in v0.1.6

func CreateSpaceOnGame(gameid uint16, kind int) EntityID

CreateSpaceOnGame creates a space with specified kind on the specified game

returns the space EntityID

func GetNilSpaceID added in v0.1.4

func GetNilSpaceID(gameid uint16) EntityID

GetNilSpaceID returns the Entity ID of nil space on the specified game

type Space added in v0.1.4

type Space = entity.Space

func CreateSpaceLocally

func CreateSpaceLocally(kind int) *Space

CreateSpaceLocally creates a space with specified kind in the local game server

returns the space EntityID

func GetNilSpace added in v0.1.4

func GetNilSpace() *Space

GetNilSpace returns the nil space on this game Nil space is a special space with Kind = 0. Nil space is the default space for all created entities. Each game has one nil space with fixed EntityID for each game, which can be acquired by calling `GetNilSpaceID`

Since nil game exists on each game with fixed EntityID, an entity can migrate to target game by calling `e.EnterSpace(GetNilSpaceID(gameid), Vector3{})`

func GetSpace added in v0.1.4

func GetSpace(id EntityID) *Space

GetSpace gets the space by ID

type Vector3 added in v0.1.4

type Vector3 = entity.Vector3

Export useful types


Path Synopsis
Package snappy implements the snappy block-based compression format.
Package snappy implements the snappy block-based compression format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL