game server framework
a actor model framework written in Go, two ways to communicate internally:
- Service discovery based on ETCD (recommend).
- Use NATS(jetStream) as a means of communication.
$ go get -u github.com/wwj31/dogactor
Code below is a simple implementation of dogactor, run it after copy to your main, or see demo/example
package main
import (
"fmt"
"time"
"github.com/wwj31/dogactor/actor"
"github.com/wwj31/dogactor/tools"
)
type PingActor struct{ actor.Base }
type PongActor struct{ actor.Base }
func main() {
system, _ := actor.NewSystem()
system.NewActor("ping", &PingActor{})
system.NewActor("pong", &PongActor{})
<-system.Stopped
fmt.Println("stop")
}
// OnInit PingActor
func (s *PingActor) OnInit() {
s.AddTimer(tools.XUID(), tools.Now().Add(time.Second), func(dt time.Duration) {
s.Send("pong", "this is a msg from pong")
}, -1)
}
func (s *PingActor) OnHandle(msg actor.Message) {
switch msg.Payload() {
case 99999:
fmt.Println(msg.GetSourceId(), msg.GetTargetId())
}
}
// OnHandle PongActor
func (s *PongActor) OnHandle(msg actor.Message) {
switch msg.Payload() {
case "this is a msg from pong":
fmt.Println(msg.GetSourceId(), msg.GetTargetId())
s.Send(msg.GetSourceId(), 99999)
}
}