我正在尝试创建一个基于 Node/Express 的 REST 服务器。如何在同一个 REST 服务器中添加 GRPC 服务器,或者它必须是完全不同的仅托管 GRPC 服务器的 NodeJS 服务器。
我正在使用 nodejs grpc 服务器流将实时数据从服务器传送到客户端。当客户端想要监听数据时,他们应该调用返回服务器流的服务器函数。然后,如果客户端想要结束监听,我想它应该调用调用对象上的 end 方法。但不幸的是,我在文档中找不到有关如何执行此操作的任何线索。我尝试过 call.end 、 call.destroy 和 call.cancel 但 end 和 destroy 什么也没做,并且 cancel 抛出以下错误。示例客户端代码如下:
function getData(token, userId) {
const data = {
token,
userId
}
let call = DataService.getData(data)
call.on('data', res => {
console.log(res)
})
call.on('status', console.log);
call.on('error', console.log);
setTimeout(() => {
console.log('destroy')
call.cancel()
}, 5000)
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Error: 1 CANCELLED: Cancelled on client
at Object.callErrorFromStatus (...grpc\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
at Object.onReceiveStatus (...grpc\node_modules\@grpc\grpc-js\build\src\client.js:330:49)
at Object.onReceiveStatus (...grpc\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:299:181)
at ...grpc\node_modules\@grpc\grpc-js\build\src\call-stream.js:145:78
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
code: 1,
details: 'Cancelled on client',
metadata: …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Sveltekit ( ) 应用程序调用 GRPC 服务(dGraph api)SvelteKit v1.0.0-next.114。
我正在使用writable的商店svelte/store,并且能够将调用 GRPC 服务的响应记录到控制台。
但是,我们使用以下命令将商店导入到我的 svelte 组件中,但出现以下错误。
<script>
import {resume_store} from '../../stores/resume';
console.log($resume_store);
</script>
Run Code Online (Sandbox Code Playgroud)
500
process is not defined
node_modules/@grpc/grpc-js/build/src/logging.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9012:28
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/metadata.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9057:21
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/call-credentials.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9231:22
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/index.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15325:30
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/util.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15483:16
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/types.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15595:18
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/dgraph.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17224:15
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/index.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17254:18
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17261:25
Run Code Online (Sandbox Code Playgroud)
我还尝试使用dgraph-js-http客户端。
观察结果(见下文)是相同的 - 即,页面在短时间内看起来符合预期。
首先,我收到一个错误,看起来与https://github.com/vitejs/vite/issues/2579上描述的相同。但是,我没有保存此内容,因此无法在此处粘贴确切的错误。
然后我尝试了
node_modulesnpm installnpm dev run然而,随后出现了以下错误。
500
Buffer2 is …Run Code Online (Sandbox Code Playgroud) 我有以下原型:
syntax = "proto3";
import "google/rpc/status.proto";
message Response {
google.rpc.Status status = 1;
}
message Request {
Type name = 1;
}
service Service {
rpc SomeMethod (Request) returns (Response);
}
Run Code Online (Sandbox Code Playgroud)
我在节点中编写一个客户端:
const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const protoFiles = require('google-proto-files');
const PROTO_PATH = path.join(__dirname, '/proto/myproto.proto');
const packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [protoFiles('rpc')],
},
);
const proto = grpc.loadPackageDefinition(packageDefinition);
const client = …Run Code Online (Sandbox Code Playgroud) protocol-buffers node.js grpc google-protocol-buffer grpc-node
如何在 ProtoBuf - Proto3 语法中将消息类型作为对象发送?
我想传输对象而不是字符串或数字。
{
name: 'One',
date: 'date',
some: 'some',
...
...
}
Run Code Online (Sandbox Code Playgroud)
syntax = "proto3";
package db;
service Proxy
{
rpc myFunction(Request) returns (Response);
}
message Request
{
string name = 1;
}
message Response
{
object keyvalue = 1;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我收到错误
throw Error("no such Type or Enum '" + path + "' in " + this);
^
Error: no such Type or Enum 'object' in Type
Run Code Online (Sandbox Code Playgroud)
——
我可以在服务器端将它转换为字符串,然后我可以在客户端 JSON.parse() 。
但我想知道,是否有更好的方法来做到这一点。
2 天前,我的 node.js 构建在 gitlab-ci 服务器上停止工作。我真的不明白为什么。更多信息:
package.json文件还没有改变时,CI构建停止工作以下是错误日志的摘录:
> grpc@1.16.0 install /builds/vallen-bridge/source/server/node_modules/grpc
> node-pre-gyp install --fallback-to-build --library=static_library
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://node-precompiled-binaries.grpc.io/grpc/v1.16.0/node-v67-linux-x64-glibc.tar.gz
node-pre-gyp WARN Pre-built binaries not found for grpc@1.16.0 and node@11.1.0 (node-v67 ABI, glibc) (falling back to source compile with node-gyp)
node-pre-gyp WARN Pre-built binaries not installable for grpc@1.16.0 and node@11.1.0 (node-v67 ABI, glibc) (falling back to source compile …Run Code Online (Sandbox Code Playgroud) gRPC 文档描述了如何使用protoc命令行程序将*.proto文件编译为某种语言,适用于除 Node 之外的所有语言。
其中,仅描述了(在撰写本文时)如何动态加载并在运行时(在幕后)生成 JS 代码。
是否可以使用该protoc程序将proto文件直接编译为JS,类似于其他语言?
我有一个 GRPC Web 客户端和一个 GRPC 服务器,我正在使用从 HTTP 1.1 到 HTTP2 的转换的特使代理。
我的服务器创建逻辑使用 TLS。代码如下:
var opts []grpc.ServerOption
creds, err := credentials.NewServerTLSFromFile("cert/server.crt", "cert/server.key")
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
server := grpc.NewServer(opts...)
Run Code Online (Sandbox Code Playgroud)
我正在从我的反应客户端调用如下:
const client = new LiveClient('http://localhost:8080')
const request = new GetLiveRequest()
request.setApi(1)
request.setTrackkey(trackKey)
// on success response
const stream = client.getLive(request, {})
stream.on('data', response => {
console.log(response);
}
Run Code Online (Sandbox Code Playgroud)
envoy.yaml 如下:
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 } …Run Code Online (Sandbox Code Playgroud) 我想使用 gRPC 服务在我的微服务之间进行通信。但是当从 Grpc 服务获得响应时,在返回一个方法之前,我想做一些修改和功能。
示例项目:https : //github.com/nestjs/nest/tree/master/sample/04-grpc
像这样:
@Get(':id')
getById(@Param('id') id: string): Observable<Hero> {
const res: Observable<Hero> = this.heroService.findOne({ id: +id });
console.log(res); res.name = res.name + 'modify string';
return res;
}
Run Code Online (Sandbox Code Playgroud)
但在 console.log 中显示以下消息而不是原始响应。
Observable { _isScalar: false, _subscribe: [Function] }
Run Code Online (Sandbox Code Playgroud) 我有一个 TypeScript 服务器尝试使用 Struct 读取 JSON 对象,但它似乎仅部分适用于包含“fields”键的对象,然后该对象需要一个对象作为值。尽管如此,Struct 应该适用于任何 JSON 对象。
使用 BloomRPC 我正在尝试以下消息:
{
"payload": {
"fields": {
"Hello": {
"whatever": 0
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
服务器读取:
{ fields: { Hello: {} } }
Run Code Online (Sandbox Code Playgroud)
如果我发送:
{
"payload": {
"anotherfield": {
"HelloWorld": {
"whatever": 0
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在服务器上得到一个空对象。
简化的 protobuf 文件如下所示:
syntax = "proto3";
import "google/protobuf/struct.proto";
// The service definition.
service TestTicketService {
rpc UpdateTicket (UpdateTicketRequest) returns (UpdateTicketResponse);
}
// The request message containing the required ticket information.
message …Run Code Online (Sandbox Code Playgroud) grpc-node ×10
grpc ×8
node.js ×7
javascript ×2
angular ×1
dgraph ×1
envoyproxy ×1
express ×1
gitlab-ci ×1
grpc-web ×1
nestjs ×1
proto3 ×1
protobuf.js ×1
svelte ×1
sveltekit ×1
typescript ×1