github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/remotewatch/node1/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	console "github.com/AsynkronIT/goconsole"
     8  	"github.com/AsynkronIT/protoactor-go/actor"
     9  	"github.com/AsynkronIT/protoactor-go/remote"
    10  )
    11  
    12  func main() {
    13  	timeout := 5 * time.Second
    14  	remote.Start("127.0.0.1:8081")
    15  
    16  	context := actor.EmptyRootContext
    17  	props := actor.PropsFromFunc(func(ctx actor.Context) {
    18  		switch msg := ctx.Message().(type) {
    19  		case *actor.Started:
    20  			log.Println("Local actor started")
    21  			pidResp, err := remote.SpawnNamed("127.0.0.1:8080", "myRemote", "remote", timeout)
    22  			if err != nil {
    23  				log.Print("Local failed to spawn remote actor")
    24  				return
    25  			}
    26  			log.Println("Local spawned remote actor")
    27  			ctx.Watch(pidResp.Pid)
    28  			log.Println("Local is watching remote actor")
    29  		case *actor.Terminated:
    30  			log.Printf("Local got terminated message %+v", msg)
    31  		}
    32  	})
    33  
    34  	context.Spawn(props)
    35  	console.ReadLine()
    36  }