因此,我使用grpc和vue-cli客户端为聊天应用程序创建了一个客户端。我的proto文件看起来像这样:
syntax = "proto3";
package chat;
option go_package = "backend/proto";
message ChatMessage {
string msg = 1;
}
service ChatService {
rpc Chat(stream ChatMessage) returns (stream ChatMessage) {}
}首先,我尝试使用以下命令创建静态客户端存根:
$ protoc --proto_path=proto --js_out=import_style=commonjs,binary:frontend/src --grpc-web_out=import_style=commonjs,mode=grpcwebtext:frontend/src proto/chat.proto但是,chat_grcp_web_pb.js文件中没有生成Chat端点。然后,我转向了从proto文件生成服务的动态方法,如基础教程here中所定义
但是在用npm run serve编译我的vue客户机时,它给出了一个错误:
ERROR Failed to compile with 1 error 9:51:48 PM
This dependency was not found:
* http2 in ./node_modules/@grpc/grpc-js/build/src/server.js
To install it, you can run: npm install --save http2旁注:我已经有了http2,但是很明显,webpack没有链接它,所以我运行了上面错误中建议的命令。但在那之后,它给出了两个警告,并且无法编译:
warning in ./node_modules/http2/lib/protocol/index.js
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
warning in ./node_modules/defaultable/defaultable.js
Critical dependency: the request of a dependency is an expression我的节点版本是16.4.2,npm版本是7.19.1,我的App.vue文件是这样的:
import * as grpc from '@grpc/grpc-js'
import * as proto_loader from '@grpc/proto-loader'
var PROTO_PATH = __dirname + '../../../proto/chat.proto'
var package_definition = proto_loader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var proto_descriptor = grpc.loadPackageDefinition(package_definition);
var chat_service = proto_descriptor.chat_service;
new chat_service.ChatService(URL, grpc.credentials.createInsecure())任何帮助解决这个问题的人都将不胜感激!
发布于 2021-07-26 23:50:43
@grpc/grpc-js库只能在Node.js (和Electron)的最新版本中运行。Node.js中内置了http2模块,因此如果您没有该模块,则可能无法在受支持的平台上运行它。
如果您尝试在浏览器中使用gRPC,则应改用grpc-web。
https://stackoverflow.com/questions/68520913
复制相似问题