Coder Social home page Coder Social logo

ivm init 支持 proto stream about jzero HOT 2 CLOSED

jaronnie avatar jaronnie commented on August 23, 2024
ivm init 支持 proto stream

from jzero.

Comments (2)

jaronnie avatar jaronnie commented on August 23, 2024
  1. 仅客户端 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.

jaronnie avatar jaronnie commented on August 23, 2024

code repo: https://github.com/jzero-io/gateway-standard-project

from jzero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.