简体   繁体   English

在没有 NodeJS 的情况下使用 GRPC-Web

[英]Using GRPC-Web without NodeJS

How do you use GRPC-Web on the browser?如何在浏览器上使用 GRPC-Web? I mean, in pure browser code, without any NodeJS involved.我的意思是,在纯浏览器代码中,不涉及任何 NodeJS。

Official example from here: https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld are mainly NodeJS oriented.来自这里的官方示例: https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld主要面向 NodeJS。

Is there way to use GRPC-Web in pure Javascript form without:有没有办法以纯 Javascript 形式使用 GRPC-Web,而无需:

const {HelloRequest, HelloReply} = require('./helloworld_pb.js');
const {GreeterClient} = require('./helloworld_grpc_web_pb.js');

Meaning, just standard <script> -way of adding Javascript dependencies?意思是,只是标准<script> -添加 Javascript 依赖项的方式? And be able to do: var client = new GreeterClient('http://localhost:8080');并且能够做到: var client = new GreeterClient('http://localhost:8080');

Yes.是的。 You need to bundle your sources with webpack.您需要将源代码与 webpack 捆绑在一起。 This step is also described in the documentation you mentioned.您提到的文档中也描述了此步骤。 At the bottom of the readme :自述文件的底部:

Just config your webpack to expose variable:只需配置您的 webpack 即可公开变量:

client.js客户端.js

...
export function instantiateGreeterClient(...) { 
    return new GreeterClient(...)
};

webpack.config.js webpack.config.js

module.exports = {
   ...
   entry: './path/to/client.js',
   output: {
      path: './bundle/js/',
      filename: 'grpc.js',
      library: {
          name: 'grpc',
          type: 'umd',
      },
   ...
}

And after that import your bundle as usual.然后像往常一样导入你的包。 Now you be able to use all defined variables in your script tag code as现在您可以在脚本标记代码中使用所有定义的变量作为

<script src="path/to/grpc.js"></script>
<script> 
    const client = grpc.instantiateGreeterClient(...)
    ...
</script>

More information can be found in webpack documentation更多信息可以在webpack 文档中找到

Use grpc-web , not @grpc/grpc-js npm package.使用grpc-web ,而不是@grpc/grpc-js npm package。

The example provided in Write Client Code uses @grpc/grpc-js , which only works on NodeJS. 编写客户端代码中提供的示例使用@grpc/grpc-js ,它仅适用于 NodeJS。

To use your protobufs and gRPC services defined in your .proto files, you need to generate your code using grpc-web .要使用.proto文件中定义的 protobuf 和 gRPC 服务,您需要使用grpc-web生成代码。 Then, import those generated files and use them.然后,导入这些生成的文件并使用它们。


Some things I learnt along the way:在此过程中我学到了一些东西:

  • You can't really use protobufjs with gprc-web in browsers, you need to use grpc-web and google-protobuf .您不能在浏览器中真正将protobufjs与 gprc-web 一起使用,您需要使用grpc-webgoogle-protobuf If someone has an alternative solution, let me know please.如果有人有替代解决方案,请告诉我。
  • The generated code in grpc-web is not very nice - it is difficult to read and doesn't have all the features of protobufs.在 grpc-web 中生成的代码不是很好——它很难阅读并且没有 protobufs 的所有功能。 You need to have a lot of Javascript and bundling experience to use it.您需要有大量的 Javascript 和捆绑经验才能使用它。
  • grpc-web is more limited than gRPC. grpc-web 比 gRPC 更受限制。 NodeJS runs gRPC, but in the browser, you have to use grpc-web, and must run a gRPC proxy to translate gRPC-web into/from gRPC. NodeJS 运行 gRPC,但在浏览器中,您必须使用 grpc-web,并且必须运行 gRPC 代理才能将 gRPC-web 转换为 gRPC 或从 gRPC 转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM