简体   繁体   English

protobuf.js 使用 CommonJS?

[英]protobuf.js using CommonJS?

I am relatively new to JS but very familiar with protobuf.我对 JS 比较陌生,但对 protobuf 非常熟悉。 I'm currently designing a web page hosted from a Java HTTP server, and would like to implement protobuf communication between them.我目前正在设计从 Java HTTP 服务器托管的 web 页面,并希望在它们之间实现 protobuf 通信。

My issue is on the browser side.我的问题在浏览器端。 After some research I found the protobuf.js git page and attempted to use this within my javascript.经过一番研究,我发现了protobuf.js git 页面并尝试在我的 javascript 中使用它。 I ran into issues firstly getting the module over HTTP because我遇到问题首先让模块超过 HTTP 因为

<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.XX/dist/protobuf.js"></script>

uses text/plaintext and fails to return.使用text/plaintext并且无法返回。 Adding a type=text/javascript just led to protobuf is not defined .添加一个type=text/javascript只是导致protobuf is not defined

I then tried to take the project source into my web root, and directly use this:然后我尝试将项目源放入我的 web 根目录,并直接使用:

<script type="text/javascript" src="./js/protobuf-js/src/index.js" ></script>

and:和:

    import * as protobuf from "./js/protobuf-js/src/index.js";

This worked and the web server returned the file.这有效,web 服务器返回了文件。 Now, this is where my understanding reaches it's limits.现在,这就是我的理解达到极限的地方。 From what I can tell from the README page on git, it distinctly says从我从 git 上的 README 页面可以看出,它清楚地说

"The library supports CommonJS and AMD loaders and also exports globally as protobuf."

If I look inside index.js I see the following:如果我查看index.js内部,我会看到以下内容:

var protobuf = module.exports = require("./index-light");

which throws a Module is not defined in ES module scope exception in browser.在浏览器Module is not defined in ES module scope

Nowhere else online could I find working examples of the protobuf.js being used in commonJS as it states in the git, it all refers to Node.js which I don't want to use as i'm using Java for the webserver side of things.我在其他任何地方都找不到在 commonJS 中使用的protobuf.js的工作示例,正如它在 git 中所说的那样,它都指的是 Node.js,我不想使用它,因为我正在为 webserver 使用 ZD52387880E79EA2817A721。

Am i being really dumb and missing something obvious?我真的很愚蠢并且错过了一些明显的东西吗?

Thanks谢谢

There are example in https://github.com/protobufjs/protobuf.js . https://github.com/protobufjs/protobuf.js中有示例。

a small example:一个小例子:

hello.proto你好.proto

syntax = "proto3";
message Test{
    string msg=1;
}

test.html测试.html

<html lang="en">
<head>
    <script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.11.3/dist/protobuf.js"></script>
</head>
<body>
  <script>
    function test(){
        protobuf.load("hello.proto", function(err, root) {
            var TestMsg = root.lookup('Test');

            var payload = {msg:'hello'};
            var result = TestMsg.verify(payload);

            if(result) throw Error(result);

            var msg = TestMsg.create(payload);
            var binMsg = TestMsg.encode(msg).finish();  // this is the binary protobuf message


            // to handle blob data from server via websocket, you need handle like below
            // event.data.arrayBuffer().then(buf =>{
            //   var msg = TestMsg.decode(new Uint8Array(buf));
            // }

            // deserialize
            var msg2 = TestMsg.decode((binMsg));
            console.log(msg2.toJSON());
            alert(msg2.msg);
        });
    }
  </script>
  <input type="button" value="test" onclick="test()">
</body>
</html>

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

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