@addrobots/grpc-web 中文文档教程

发布于 5 年前 浏览 5 项目主页 更新于 2 年前

@improbable-eng/grpc-web

用于从浏览器发出 gRPC-Web 请求的

库此库适用于从 Web 浏览器或 NodeJS 使用 JavaScript 和 TypeScript(请参阅NodeJS 的用法)。

注意:这仅在服务器支持 gRPC-Web 时有效/em>

Golang gRPC-Web 中间件和基于 Golang 的 gRPC-Web 代理可在此处获取

请参阅完整的 gRPC-Web README 了解已知限制。

Installation

@improbable-eng/grpc-web 具有 google-protobuf@types/google-protobuf 的对等依赖项。

npm install google-protobuf @types/google-protobuf @improbable-eng/grpc-web --save

Example Project

有一个 example project available here

Usage Overview

import {grpc} from "@improbable-eng/grpc-web";

// Import code-generated data structures.
import {BookService} from "./generated/proto/examplecom/library/book_service_pb_service";
import {GetBookRequest} from "./generated/proto/examplecom/library/book_service_pb";

const getBookRequest = new GetBookRequest();
getBookRequest.setIsbn(60929871);
grpc.unary(BookService.GetBook, {
  request: getBookRequest,
  host: host,
  onEnd: res => {
    const { status, statusMessage, headers, message, trailers } = res;
    if (status === grpc.Code.OK && message) {
      console.log("all ok. got book: ", message.toObject());
    }
  }
});
  • Requests can be aborted/cancelled before they complete:
const request = grpc.unary(BookService.GetBook, { ... });
request.cancel();

Available Request Functions

发出 gRPC 请求的三个函数:

grpc.unary

这是一个方便的函数,用于发出由单个请求消息组成的请求和单个响应消息。 它只能与一元方法一起使用。

rpc GetBook(GetBookRequest) returns (Book) {}

grpc.invoke

这是一个方便的函数,用于发出由单个请求消息和响应消息流(服务器流)组成的请求。 它也可以与一元方法一起使用。

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}

grpc.client

grpc.client 返回一个客户端。 根据传输兼容性,此客户端能够发送多个请求消息(客户端流)并接收多个响应消息(服务器流)。 它可以与任何类型的方法一起使用,但会强制限制一元方法的消息发送。

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}
rpc LogReadPages(stream PageRead) returns (google.protobuf.Empty) {}
rpc ListenForBooks(stream QueryBooksRequest) returns (stream Book) {}

Usage with NodeJS

参考 grpc-web-node-http-transport

All Docs

@improbable-eng/grpc-web

Library for making gRPC-Web requests from a browser

This library is intended for both JavaScript and TypeScript usage from a web browser or NodeJS (see Usage with NodeJS).

Note: This only works if the server supports gRPC-Web

A Golang gRPC-Web middleware and a Golang-based gRPC-Web proxy are available here.

Please see the full gRPC-Web README for known limitations.

Installation

@improbable-eng/grpc-web has peer dependencies of google-protobuf and @types/google-protobuf.

npm install google-protobuf @types/google-protobuf @improbable-eng/grpc-web --save

Example Project

There is an example project available here

Usage Overview

import {grpc} from "@improbable-eng/grpc-web";

// Import code-generated data structures.
import {BookService} from "./generated/proto/examplecom/library/book_service_pb_service";
import {GetBookRequest} from "./generated/proto/examplecom/library/book_service_pb";

const getBookRequest = new GetBookRequest();
getBookRequest.setIsbn(60929871);
grpc.unary(BookService.GetBook, {
  request: getBookRequest,
  host: host,
  onEnd: res => {
    const { status, statusMessage, headers, message, trailers } = res;
    if (status === grpc.Code.OK && message) {
      console.log("all ok. got book: ", message.toObject());
    }
  }
});
  • Requests can be aborted/cancelled before they complete:
const request = grpc.unary(BookService.GetBook, { ... });
request.cancel();

Available Request Functions

There are three functions for making gRPC requests:

grpc.unary

This is a convenience function for making requests that consist of a single request message and single response message. It can only be used with unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}

grpc.invoke

This is a convenience function for making requests that consist of a single request message and a stream of response messages (server-streaming). It can also be used with unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}

grpc.client

grpc.client returns a client. Dependant upon transport compatibility this client is capable of sending multiple request messages (client-streaming) and receiving multiple response messages (server-streaming). It can be used with any type of method, but will enforce limiting the sending of messages for unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}
rpc LogReadPages(stream PageRead) returns (google.protobuf.Empty) {}
rpc ListenForBooks(stream QueryBooksRequest) returns (stream Book) {}

Usage with NodeJS

Refer to grpc-web-node-http-transport.

All Docs

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文