gPRC如何将阵列数据从服务器发送到客户端[gPRC + Node.js]

时间:2018-12-26 19:55:22

标签: grpc grpc-node

我正在尝试将一个数组数据对象从server.js发送到client.js,但是我不确定。

发送此数据类型的正确方法是什么

- string
- number
- Object
- array

我正在尝试从主机向客户端发送简单的数组数据。我什至不想想象要发送更复杂的数据,例如 Object

有人可以向我展示一个简单的工作示例,我可以从server.js发送此数据

[
  { id: '1', title: 'Note 1', content: 'Content 1'},
  { id: '2', title: 'Note 2', content: 'Content 2'},
  { id: '3', title: 'Note 3', content: 'Content 3'}
]

在客户端上,如果我运行

,我希望看到此响应
  

节点。\ client.js

[
  { id: '1', title: 'Note 1', content: 'Content 1'},
  { id: '2', title: 'Note 2', content: 'Content 2'},
  { id: '3', title: 'Note 3', content: 'Content 3'}
]

notes.proto

syntax = "proto3";

package notes;

service NoteService {
    rpc GetNoteList (Empty) returns (NoteList) {}  <--- this not workig
    rpc GetNoteItem (Empty) returns (Note) {} <--- this works
}
message Empty {}

message Note {
    string id = 1;
    string title = 2;
    string content = 3;
}

message NoteList {
    repeated Note notes = 1;
}

server.js

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');

const PROTO_PATH = __dirname + '/../../protos/notes.proto';
// const notesProto = grpc.load('notes.proto')

const packageDefinition = protoLoader.loadSync(
  PROTO_PATH,
  { 
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  }
);

const notesProto = grpc.loadPackageDefinition(packageDefinition).notes;



//  returns a list of notes.
const getNoteList = (call, callback) => {
  // mock data
  const notes = [
    { id: '1', title: 'Note 1', content: 'Content 1'},
    { id: '2', title: 'Note 2', content: 'Content 2'},
    { id: '3', title: 'Note 3', content: 'Content 3'},
  ];
  callback(null, { message: notes });
}


function getNoteItem(call, callback) {
  const data = { id: '1', title: 'Note 1', content: 'Content 1'};
  return callback(null, data)
}

/**
 * Starts an RPC server that receives requests for the Greeter service at the
 * sample server port
 */
function main() {
  var server = new grpc.Server();
  server.addService(notesProto.NoteService.service, {
    GetNoteList: getNoteList,
    GetNoteItem: getNoteItem
  });
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  console.log('Server running at http://127.0.0.1:50051')
  server.start();
}

main();

client.js

// var PROTO_PATH = __dirname + '/../../protos/model.proto';
var PROTO_PATH = __dirname + '/../../protos/notes.proto';


var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
  PROTO_PATH,
  {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  }
);
const notesProto = grpc.loadPackageDefinition(packageDefinition).notes;

function main() {
  const client = new notesProto.NoteService('localhost:50051', grpc.credentials.createInsecure());
  var user;

  if (process.argv.length >= 3) {
    user = process.argv[2];
  } else {
    user = 'world';
  }

  // console.log({user : user});

    // expected to return array of objects
  client.getNoteList('test', (err, res) => {
    if (!err) {
      console.log('list1: ', res);
      console.log('list2: ', res.message);
    } else {
      console.error(err);
    }
  });


  // get a single item
  client.getNoteItem('test', (err, res) => {
    if (!err) {
      console.log('getNoteItem res: ', res);
    } else {
      console.error(err);
    }
  });


}

main();

输出

PS C:\dev\george\tests\GRPC\grpc-test\server\node> node .\client.js
getNoteItem res:  { id: '1', title: 'Note 1', content: 'Content 1' }
list1:  { notes: [] }
list2:  undefined

2 个答案:

答案 0 :(得分:0)

您是否尝试过为每个项目调用回调?

答案 1 :(得分:0)

我发现了问题所在,就是这个

// returns a list of notes.
const getNoteList = (call, callback) => {

  // mock data
  const data = [
    { id: '1', title: 'Note 1', content: 'Content 1'},
    { id: '2', title: 'Note 2', content: 'Content 2'},
    { id: '3', title: 'Note 3', content: 'Content 3'},
  ];
  callback(null, { notes: data });  <---
}