Comments (2)
- 仅客户端 stream
v1 logic
package hellologic
import (
"context"
"io"
"strings"
"simplegateway/internal/pb/hellopb"
"simplegateway/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SayHelloLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSayHelloLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SayHelloLogic {
return &SayHelloLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SayHelloLogic) SayHello(stream hellopb.Hello_SayHelloServer) error {
var messages []string
for {
// 接收客户端发送的消息
req, err := stream.Recv()
if err == io.EOF {
// 客户端流结束
break
} else if req != nil {
messages = append(messages, req.Message)
}
}
// 处理收到的消息并生成回复
reply := &hellopb.SayHelloResponse{
Message: strings.Join(messages, ""),
}
// 发送回复给客户端
if err := stream.SendAndClose(reply); err != nil {
l.Logger.Errorf("failed to send message: %v", err)
return err
}
return nil
}
client
package main
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/zrpc"
"log"
"simplegateway/rpclient-go/hello"
"simplegateway/rpclient-go/pb/hellopb"
"strconv"
)
func main() {
client, err := zrpc.NewClient(zrpc.NewDirectClientConf([]string{"localhost:8000"}, "", ""))
if err != nil {
panic(err)
}
helloSvr := hello.NewHello(client)
stream, err := helloSvr.SayHello(context.Background())
if err != nil {
panic(err)
}
for i := 0; i < 5; i++ {
req := &hellopb.SayHelloRequest{
Message: "Hello" + strconv.Itoa(i),
}
if err := stream.Send(req); err != nil {
log.Fatalf("could not send request: %v", err)
}
}
err = stream.CloseSend()
if err != nil {
log.Fatalf("could not close stream: %v", err)
}
// 接收服务器的响应
var res hellopb.SayHelloResponse
err = stream.RecvMsg(&res)
if err != nil {
log.Fatalf("could not receive response: %v", err)
}
fmt.Println(res.Message)
}
v2 logic
package hellov2logic
import (
"context"
"google.golang.org/protobuf/proto"
"io"
"simplegateway/internal/logic/hello"
"simplegateway/internal/pb/hellopb"
"simplegateway/internal/pb/hellov2pb"
"simplegateway/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
// SayHelloServerAdapter adapter to convert hellov2pb.Hellov2_SayHelloServer to hellopb.Hello_SayHelloServer
type SayHelloServerAdapter struct {
hellov2pb.Hellov2_SayHelloServer
}
func (s *SayHelloServerAdapter) SendAndClose(response *hellopb.SayHelloResponse) error {
marshal, err := proto.Marshal(response)
if err != nil {
return err
}
var newResp hellov2pb.SayHelloResponse
err = proto.Unmarshal(marshal, &newResp)
if err != nil {
return err
}
return s.SendMsg(&newResp)
}
func (s *SayHelloServerAdapter) Recv() (*hellopb.SayHelloRequest, error) {
for {
newIn, err := s.Hellov2_SayHelloServer.Recv()
if err == io.EOF {
return nil, io.EOF
}
if err != nil {
return nil, err
}
marshal, err := proto.Marshal(newIn)
if err != nil {
return nil, err
}
var oldIn hellopb.SayHelloRequest
err = proto.Unmarshal(marshal, &oldIn)
if err != nil {
return nil, err
}
return &oldIn, nil
}
}
type SayHelloLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSayHelloLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SayHelloLogic {
return &SayHelloLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SayHelloLogic) SayHello(stream hellov2pb.Hellov2_SayHelloServer) error {
logic := hellologic.NewSayHelloLogic(l.ctx, l.svcCtx)
// Create the adapter
adapter := &SayHelloServerAdapter{
stream,
}
return logic.SayHello(adapter)
}
from jzero.
code repo: https://github.com/jzero-io/gateway-standard-project
from jzero.
Related Issues (20)
- 支持rpc中生成缓存的时候在key前面自定义参数 HOT 2
- 当根目录存在 go.work 和 go.work.sum 时, jzero gen 无法生成
- windows 执行 jzero gen swagger 失败 HOT 1
- 执行 jzero -h 会生成一个多余的 go.mod 文件 HOT 2
- 当 api 返回的类型为非 api type. e.g. string, gen sdk 有问题
- client-go fake response type redeclared in this packag because of same handler name
- proto 的字段验证功能 HOT 1
- 框架需要 api 版本控制管理能力 HOT 2
- jzero gen --remove-suffix error HOT 1
- 优化 docker buildx 的速度 HOT 1
- 持续增强项目规范 HOT 3
- 自动更新 Gateway.Upstreams.0.Grpc.ProtoSets 在 windows 上使用了 \ 隔离符, 导致 docker 编译无法通过
- 采用了 logtoconsole.Must 后,在 debug 模式下,logx 无法及时输出到控制台 HOT 1
- proto validator希望支持自定义错误message HOT 3
- option go_package 路径问题 HOT 1
- gateway 项目运行需要依赖 pb 文件 HOT 1
- 对于 gateway 框架的路由分组的 middleware 控制 HOT 4
- docker build failing for example. HOT 6
- docker makefile changes. HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jzero.