v1 接收 Proto 架构类型的消息(已废弃)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
(已弃用)接收 Proto 架构类型的消息
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# v1 Receive messages of Proto schema type (DEPRECATED)\n\n(DEPRECATED) Receive messages of Proto schema type\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Go API\nreference documentation](https://godoc.org/cloud.google.com/go/pubsub).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"sync\"\n \t\"time\"\n\n \t\"cloud.google.com/go/pubsub\"\n \tstatepb \"github.com/GoogleCloudPlatform/golang-samples/internal/pubsub/schemas\"\n \t\"google.golang.org/protobuf/encoding/protojson\"\n \t\"google.golang.org/protobuf/proto\"\n )\n\n func subscribeWithProtoSchema(w io.Writer, projectID, subID, protoFile string) error {\n \t// projectID := \"my-project-id\"\n \t// subID := \"my-sub\"\n \t// protoFile = \"path/to/a/proto/schema/file(.proto)/formatted/in/protocol/buffers\"\n \tctx := context.Background()\n \tclient, err := pubsub.https://cloud.google.com/go/docs/reference/cloud.google.com/go/pubsub/latest/index.html#cloud_google_com_go_pubsub_Client_NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"pubsub.NewClient: %w\", err)\n \t}\n\n \t// Create an instance of the message to be decoded (a single U.S. state).\n \tstate := &statepb.State{}\n\n \tsub := client.Subscription(subID)\n \tctx2, cancel := context.WithTimeout(ctx, 10*time.Second)\n \tdefer cancel()\n\n \tvar mu sync.Mutex\n \tsub.Receive(ctx2, func(ctx context.Context, msg *pubsub.Message) {\n \t\tmu.Lock()\n \t\tdefer mu.Unlock()\n \t\tencoding := msg.Attributes[\"googclient_schemaencoding\"]\n\n \t\tif encoding == \"BINARY\" {\n \t\t\tif err := proto.Unmarshal(msg.Data, state); err != nil {\n \t\t\t\tfmt.Fprintf(w, \"proto.Unmarshal err: %v\\n\", err)\n \t\t\t\tmsg.Nack()\n \t\t\t\treturn\n \t\t\t}\n \t\t\tfmt.Printf(\"Received a binary-encoded message:\\n%#v\\n\", state)\n \t\t} else if encoding == \"JSON\" {\n \t\t\tif err := protojson.Unmarshal(msg.Data, state); err != nil {\n \t\t\t\tfmt.Fprintf(w, \"proto.Unmarshal err: %v\\n\", err)\n \t\t\t\tmsg.Nack()\n \t\t\t\treturn\n \t\t\t}\n \t\t\tfmt.Fprintf(w, \"Received a JSON-encoded message:\\n%#v\\n\", state)\n \t\t} else {\n \t\t\tfmt.Fprintf(w, \"Unknown message type(%s), nacking\\n\", encoding)\n \t\t\tmsg.Nack()\n \t\t\treturn\n \t\t}\n \t\tfmt.Fprintf(w, \"%s is abbreviated as %s\\n\", state.Name, state.PostAbbr)\n \t\tmsg.Ack()\n \t})\n \treturn nil\n }\n\n### Ruby\n\n\nBefore trying this sample, follow the Ruby setup instructions in the\n[Pub/Sub quickstart using\nclient libraries](/pubsub/docs/quickstart-client-libraries).\n\n\nFor more information, see the\n[Pub/Sub Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html).\n\n\nTo authenticate to Pub/Sub, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # subscription_id = \"your-subscription-id\"\n\n pubsub = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud.html.new\n\n subscription = pubsub.subscription subscription_id\n\n subscriber = subscription.listen do |received_message|\n encoding = received_message.attributes[\"googclient_schemaencoding\"]\n case encoding\n when \"BINARY\"\n state = Utilities::StateProto.decode received_message.data\n puts \"Received a binary-encoded message:\\n#{state}\"\n when \"JSON\"\n require \"json\"\n state = Utilities::StateProto.decode_json received_message.data\n puts \"Received a JSON-encoded message:\\n#{state}\"\n else\n \"Received a message with no encoding:\\n#{received_message.message_id}\"\n end\n received_message.acknowledge!\n end\n\n subscriber.start\n # Let the main thread sleep for 60 seconds so the thread for listening\n # messages does not quit\n sleep 60\n subscriber.stop.wait!\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=pubsub)."]]