Criado cliente

This commit is contained in:
Luiz F Picolo 2024-06-05 22:09:10 -04:00
parent f916e8e988
commit ef309362e8
3 changed files with 35 additions and 1 deletions

27
client.js Normal file
View File

@ -0,0 +1,27 @@
import { credentials, loadPackageDefinition } from "@grpc/grpc-js";
import { loadSync } from "@grpc/proto-loader";
// Ler as definições do protobuffer
const tasksDefs = loadSync("./tasks.proto");
const tasksProto = loadPackageDefinition(tasksDefs);
const clientGRPC = new tasksProto.TaskService('127.0.0.1:5050', credentials.createInsecure());
clientGRPC.findAll({}, (err, tasks) => {
if (err){
console.log(err);
return;
}
console.log(tasks)
});
const obj = {id: 2, title: 'picolo'}
clientGRPC.insertOne(obj, (err, task) => {
if (err){
console.log(err);
return;
}
console.log(task)
})

View File

@ -14,6 +14,12 @@ const grpcServer = new Server();
grpcServer.addService(tasksProto.TaskService.service, { grpcServer.addService(tasksProto.TaskService.service, {
findAll: (call, callBack) => { findAll: (call, callBack) => {
callBack(null, { tasks: tasks }) callBack(null, { tasks: tasks })
},
insertOne: (call, callBack) => {
const obj = call.request
tasks.push(obj)
callBack(null, obj)
} }
}) })

View File

@ -4,6 +4,7 @@ syntax = "proto3";
// Métodos que serão invocados remotamente // Métodos que serão invocados remotamente
service TaskService { service TaskService {
rpc FindAll( Empty ) returns ( TaskList ); rpc FindAll( Empty ) returns ( TaskList );
rpc InsertOne( Task ) returns ( Task );
} }
// Mensagem (Message) // Mensagem (Message)