0

我正在研究节点运行时的 gRPC 服务器-客户端编程。

我在客户端流式传输 rpc 中遇到错误。请参阅以下 rpc 方法签名。

service RouteGuide{
  rpc DataStreaming(stream File) returns (Stats) {}
}

message Stats{
  string msg=1;
}

message File{
  bytes chk=1;
}

我想将文件从客户端上传到服务器。所以我定义了客户端流 rpc。

问题是文件上传只会第一次成功。

当我尝试上传另一个文件时,出现错误。在结束错误之后写。

我认为我没有很好地处理流。任何人都可以帮助为什么会发生这种情况?谢谢!

// server.js
"use strict";

const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const path = require("path");
const fs = require("fs");
const stream = require("stream");

const PROTO_PATH = path.join(__dirname, "proto", "route.proto"); //    path.resolve("proto", "route.proto")
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
  keepCase: false,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true
});
const routeguide = grpc.loadPackageDefinition(packageDefinition).routeguide;

const myTransformStream = new stream.Transform({
  objectMode: true,
  transform(data, enc, cb) {
    cb(null, data.chk.toString());
  }
});

function dataStreaming(strm, cb) {
  console.log("server : streaming function");
  stream.pipeline(
    strm,
    myTransformStream,
    fs.createWriteStream("output.txt"),
    err => {
      if (err) {
        console.log(`server side error : ${err}`);
        cb(err);
      } else {
        console.log("server side no error");
        cb(null, "server side finish");
      }
    }
  );
}

function getServer() {
  const server = new grpc.Server();
  server.addService(routeguide.RouteGuide.service, {
    DataStreaming: dataStreaming
  });
  return server;
}

if (require.main === module) {
  const routeServer = getServer();
  routeServer.bind("localhost:3333", grpc.ServerCredentials.createInsecure());
  routeServer.start();
}

==============

// client.js
"use strict";

const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const path = require("path");
const fs = require("fs");
const stream = require("stream");

const PROTO_PATH = path.join(__dirname, "proto", "route.proto"); //    path.resolve("proto", "route.proto")
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
  keepCase: false,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true
});
const routeguide = grpc.loadPackageDefinition(packageDefinition).routeguide;
const client = new routeguide.RouteGuide(
  "localhost:3333",
  grpc.credentials.createInsecure()
);

const MyTransform = new stream.Transform({
  objectMode: true,
  transform(chk, enc, cb) {
    cb(null, { chk: chk });
  }
});

function runDataStreaming() {
  console.log("inside run-data-streaming()");

  const strm = client.dataStreaming((err, ret) => {
    if (err) {
      console.log("client : file transfer failed.");
      console.log(err);
    } else {
      console.log("client : file transfer succeeded.");
    }
  });

  stream.pipeline(fs.createReadStream("test.txt"), MyTransform, strm, err => {
    if (err) {
      console.log(err.message);
    } else {
      console.log("pipeline succeeded");
    }
  });
}

if (require.main === module) {
  runDataStreaming();
}
4

1 回答 1

0

我解决了我的问题。有一些问题。我会解释的。

  1. myTransformStreamserver.js 中的全局变量是问题所在。我将代码移动到dataStreaming函数中。

  2. 我还更改了关闭、销毁选项。详细地说,我打开了autocloseemitclose选项在fs.createReadStream()fs.createWriteStream() 和...打开emitcloseautodestroy选项在转换流(myTransformStreammyTransform

于 2020-01-14T14:49:27.500 回答