-
Notifications
You must be signed in to change notification settings - Fork 11
/
example1.go
51 lines (43 loc) · 1.02 KB
/
example1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"time"
"github.com/wwj31/godactor/actor"
"github.com/wwj31/godactor/tools"
)
type PingActor struct{ actor.Base }
type PongActor struct{ actor.Base }
func main() {
//log.Init(log.TAG_DEBUG_I, nil, "./_log", "demo", 1)
system, _ := actor.NewSystem()
ping := actor.New("ping", &PingActor{})
pong := actor.New("pong", &PongActor{})
system.Regist(ping)
system.Regist(pong)
<-system.CStop
fmt.Println("stop")
}
// PingActor
func (s *PingActor) OnInit() {
s.AddTimer(tools.UUID(), 2*time.Second, func(dt int64) {
s.Send("pong", "this is data")
}, -1)
s.AddTimer(tools.UUID(), 10*time.Second, func(dt int64) {
s.System().Stop()
}, 1)
}
func (s *PingActor) OnHandleMessage(sourceId, targetId string, msg interface{}) {
switch msg {
case 99999:
fmt.Println(sourceId, targetId)
fmt.Println()
}
}
//PongActor
func (s *PongActor) OnHandleMessage(sourceId, targetId string, msg interface{}) {
switch msg {
case "this is data":
fmt.Println(sourceId, targetId)
s.Send(sourceId, 99999)
}
}