12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package util
- import (
- "app.yhyue.com/BP/queued/proto"
- "context"
- "google.golang.org/grpc"
- "log"
- "time"
- )
- /**
- 接收端
- */
- type Reciver struct {
- Id string
- Channel string
- QueueServer string
- C chan<- *proto.PubReq
- }
- func NewReciver(server string, channel string, ch chan<- *proto.PubReq) *Reciver {
- uuid, _ := makeUuid()
- return &Reciver{
- Id: uuid,
- Channel: channel,
- C: ch,
- QueueServer: server,
- }
- }
- func (r *Reciver) Run() {
- for {
- conn, err := grpc.Dial(r.QueueServer, grpc.WithInsecure())
- if err != nil {
- time.Sleep(1 * time.Second)
- log.Fatalf("did not connect: %v", err)
- }
- c := proto.NewQueueServiceClient(conn)
- stream, _ := c.Receive(context.Background(), &proto.RecvReq{
- Sender: r.Id,
- ChannelId: r.Channel,
- })
- //
- Lab:
- for {
- msg, err := stream.Recv()
- if err != nil {
- log.Print(err.Error())
- break Lab
- }
- r.C <- msg
- }
- _ = conn.Close()
- time.Sleep(3 * time.Second)
- }
- }
|