1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| const grpc = require('@grpc/grpc-js') const protoLoader = require('@grpc/proto-loader')
const protoPath = "proto/hello.proto" const proto = grpc.loadPackageDefinition(protoLoader.loadSync(protoPath, { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true })) const client = new proto.hello.Hello('localhost:34067', grpc.credentials.createInsecure())
for (let i = 0; i < 5; i++) { let time = Date.now() client.call({ name: "cyl" }, (err, response) => { console.log("call data t:", Date.now() - time, response) }) }
let streamTime = Date.now() const stream = client.stream({ count: 5 }) stream.on('data', response => { console.log("stream data t:", Date.now() - streamTime, response) streamTime = Date.now() }) stream.on('end', () => { console.log("stream end t:", Date.now() - streamTime) })
const pingPong = client.pingPong() let pingPongTime const sendPingPong = () => { pingPongTime = Date.now() pingPong.write({ stroke: pingPongTime }) } pingPong.on('data', response => { console.log("pingPong data t:", Date.now() - pingPongTime, response) setTimeout(sendPingPong, 1000) }) sendPingPong()
|