reciver.go 991 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package util
  2. import (
  3. "app.yhyue.com/BP/queued/proto"
  4. "context"
  5. "google.golang.org/grpc"
  6. "log"
  7. "time"
  8. )
  9. /**
  10. 接收端
  11. */
  12. type Reciver struct {
  13. Id string
  14. Channel string
  15. QueueServer string
  16. C chan<- *proto.PubReq
  17. }
  18. func NewReciver(server string, channel string, ch chan<- *proto.PubReq) *Reciver {
  19. uuid, _ := makeUuid()
  20. return &Reciver{
  21. Id: uuid,
  22. Channel: channel,
  23. C: ch,
  24. QueueServer: server,
  25. }
  26. }
  27. func (r *Reciver) Run() {
  28. for {
  29. conn, err := grpc.Dial(r.QueueServer, grpc.WithInsecure())
  30. if err != nil {
  31. time.Sleep(1 * time.Second)
  32. log.Fatalf("did not connect: %v", err)
  33. }
  34. c := proto.NewQueueServiceClient(conn)
  35. stream, _ := c.Receive(context.Background(), &proto.RecvReq{
  36. Sender: r.Id,
  37. ChannelId: r.Channel,
  38. })
  39. //
  40. Lab:
  41. for {
  42. msg, err := stream.Recv()
  43. if err != nil {
  44. log.Print(err.Error())
  45. break Lab
  46. }
  47. r.C <- msg
  48. }
  49. _ = conn.Close()
  50. time.Sleep(3 * time.Second)
  51. }
  52. }