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

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"runtime"
     6  
     7  	console "github.com/AsynkronIT/goconsole"
     8  	"github.com/AsynkronIT/protoactor-go/actor"
     9  	"github.com/AsynkronIT/protoactor-go/examples/distributedchannels/messages"
    10  	"github.com/AsynkronIT/protoactor-go/remote"
    11  )
    12  
    13  func main() {
    14  	runtime.GOMAXPROCS(runtime.NumCPU())
    15  	remote.Start("127.0.0.1:8080")
    16  	// create the channel
    17  	channel := make(chan *messages.MyMessage)
    18  
    19  	// create an actor receiving messages and pushing them onto the channel
    20  	props := actor.PropsFromFunc(func(context actor.Context) {
    21  		if msg, ok := context.Message().(*messages.MyMessage); ok {
    22  			channel <- msg
    23  		}
    24  	})
    25  
    26  	// define root context
    27  	rootContext := actor.EmptyRootContext
    28  
    29  	// spawn
    30  	rootContext.SpawnNamed(props, "MyMessage")
    31  
    32  	// consume the channel just like you use to
    33  	go func() {
    34  		for msg := range channel {
    35  			log.Println(msg)
    36  		}
    37  	}()
    38  
    39  	console.ReadLine()
    40  }