From 9ee78f937d5be67058882dd3590f89da35bca239 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 3 May 2021 22:52:30 +0300 Subject: - Initial broadcast commit Signed-off-by: Valery Piashchynski --- plugins/broadcast/root/tests/go-client.go | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 plugins/broadcast/root/tests/go-client.go (limited to 'plugins/broadcast/root/tests/go-client.go') diff --git a/plugins/broadcast/root/tests/go-client.go b/plugins/broadcast/root/tests/go-client.go new file mode 100644 index 00000000..21442a01 --- /dev/null +++ b/plugins/broadcast/root/tests/go-client.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "os" + + "github.com/spiral/broadcast/v2" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/service/rpc" + "golang.org/x/sync/errgroup" +) + +type logService struct { + broadcast *broadcast.Service + stop chan interface{} +} + +func (l *logService) Init(service *broadcast.Service) (bool, error) { + l.broadcast = service + + return true, nil +} + +func (l *logService) Serve() error { + l.stop = make(chan interface{}) + + client := l.broadcast.NewClient() + if err := client.SubscribePattern("tests/*"); err != nil { + return err + } + + logFile, _ := os.Create("log.txt") + + g := &errgroup.Group{} + g.Go(func() error { + for msg := range client.Channel() { + _, err := logFile.Write([]byte(fmt.Sprintf( + "%s: %s\n", + msg.Topic, + string(msg.Payload), + ))) + if err != nil { + return err + } + + err = logFile.Sync() + if err != nil { + return err + } + } + return nil + }) + + <-l.stop + err := logFile.Close() + if err != nil { + return err + } + + err = client.Close() + if err != nil { + return err + } + + return g.Wait() +} + +func (l *logService) Stop() { + close(l.stop) +} + +func main() { + rr.Container.Register(rpc.ID, &rpc.Service{}) + rr.Container.Register(broadcast.ID, &broadcast.Service{}) + rr.Container.Register("log", &logService{}) + + rr.Execute() +} -- cgit v1.2.3