要发送一个数组,您必须构造一个适当对象的集合。我假设您的原型看起来像这样:
message EventProfile {
repeated Event events = 1;
}
message Event {
string title = 1;
}
message EventResponse {}
service EventService {
rpc sendEvent(EventProfile) returns (EventResponse) {}
}
然后从proto发送生成代码后可以这样实现:
let myEvents = [{title:"Title A"}, {title:"Title B"}]; // example of data you want to send collected in the app
let client = new EventServiceClient("http://localhost:8000", null, null); // example of client setup
let protoEventsList = [];
myEvents.forEach((event) => {
let protoEvent = new Event();
protoEvent.setTitle(event.title);
protoEventsList.push(protoEvent);
})
let request = new EventProfile();
request.setEventsList(protoEventsList);
client.sendEvent(request, {}, () => {
console.log("Done");
})