go-zero/example/rpc/server/stream/server.go

50 lines
933 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package main
import (
"fmt"
"io"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/example/rpc/remote/stream"
2020-09-18 11:41:52 +08:00
"github.com/tal-tech/go-zero/zrpc"
2020-07-26 17:09:05 +08:00
"google.golang.org/grpc"
)
type StreamGreetServer int
func (gs StreamGreetServer) Greet(s stream.StreamGreeter_GreetServer) error {
ctx := s.Context()
for {
select {
case <-ctx.Done():
fmt.Println("cancelled by client")
return ctx.Err()
default:
req, err := s.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
fmt.Println("=>", req.Name)
greet := "hello, " + req.Name
fmt.Println("<=", greet)
s.Send(&stream.StreamResp{
Greet: greet,
})
}
}
}
func main() {
2020-09-18 11:41:52 +08:00
var c zrpc.RpcServerConf
2020-07-26 17:09:05 +08:00
conf.MustLoad("etc/config.json", &c)
2020-09-18 11:41:52 +08:00
server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) {
2020-07-26 17:09:05 +08:00
stream.RegisterStreamGreeterServer(grpcServer, StreamGreetServer(0))
})
server.Start()
}