所以我正在使用 grpc 和 vue-cli 客户端创建一个用于聊天应用程序的客户端。我的原型文件如下所示:
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
没有在 chat_grcp_web_pb.js 文件中生成端点。然后我转向了从原始文件生成服务的动态方式,如基础教程中定义的那样
但是在用 编译我的 vue 客户端时npm run serve
,它给出了一个错误:
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 没有链接它,所以我运行了上面错误中建议的命令。但之后它给出了 2 个警告并且无法编译:
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
我的node版本是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())
任何解决此问题的帮助将不胜感激!