0

我正在尝试将一个数组数据对象从 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'}
]

笔记.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;
}

服务器.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();

客户端.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
4

2 回答 2

0

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

于 2018-12-26T20:11:39.093 回答
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 });  <---
}
于 2018-12-26T21:08:35.917 回答