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