【问题标题】:gPRC how to send array data from the server to the client [gPRC + Node.js]gPRC如何将数组数据从服务器发送到客户端[gPRC + Node.js]
【发布时间】:2018-12-26 19:55:21
【问题描述】:

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

【问题讨论】:

    标签: grpc grpc-node


    【解决方案1】:

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

    【讨论】:

    • 什么意思?你能更精确一点吗?
    【解决方案2】:

    我发现了,问题是这样的

    // 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 });  <---
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2013-04-11
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多