You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/credentials/insecure"
  9. )
  10. func main() {
  11. err := pb.WithBrokerGrpcClient(true,
  12. "localhost:17777",
  13. grpc.WithTransportCredentials(insecure.NewCredentials()),
  14. func(client mq_pb.SeaweedMessagingClient) error {
  15. subClient, err := client.Subscribe(context.Background(), &mq_pb.SubscribeRequest{
  16. Init: &mq_pb.SubscribeRequest_InitMessage{
  17. Topic: &mq_pb.Topic{
  18. Namespace: "test",
  19. Name: "test",
  20. },
  21. },
  22. })
  23. if err != nil {
  24. return err
  25. }
  26. for {
  27. resp, err := subClient.Recv()
  28. if err != nil {
  29. return err
  30. }
  31. if resp.GetCtrl() != nil {
  32. if resp.GetCtrl().Error != "" {
  33. return fmt.Errorf("ctrl error: %v", resp.GetCtrl().Error)
  34. }
  35. }
  36. if resp.GetData() != nil {
  37. println(string(resp.GetData().Key), "=>", string(resp.GetData().Value))
  38. }
  39. }
  40. return nil
  41. })
  42. if err != nil {
  43. fmt.Println(err)
  44. }
  45. }