dogactor

module
v1.4.26 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: GPL-3.0

README

Go Report Card GitHub closed pull requests GitHub go.mod Go version (subdirectory of monorepo)

game server framework

a actor model framework written in Go, two ways to communicate internally:

  1. Service discovery based on ETCD.
  2. Use NATS(jetStream) as a means of communication(default).

img

1.Installation

To start using dogactor, install Go and run go get:
$ go get -u github.com/wwj31/dogactor

2.Quick Start

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)
	}
}

Jump to

Keyboard shortcuts

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