grpcTest.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Subject, Subscription, from, interval, take } from 'rxjs';
  2. import * as grpc from '@grpc/grpc-js';
  3. import { readFileSync } from 'fs';
  4. import { message_proto } from '../services/protos/server.proto';
  5. import { Status } from '@grpc/grpc-js/build/src/constants';
  6. import assert = require('assert');
  7. const messagesJSON: any = readFileSync('payload.json')
  8. let parsedMessages: any[] = JSON.parse(messagesJSON) // load the fake messages generated for this trial
  9. let hostServer: string = 'localhost:3000'
  10. let targetServer: string = 'localhost:3000'
  11. let array: any[] = [] // Used for testing
  12. let intervalToStreamOutGoingMessage: number = 15
  13. /* Checking the values by the end of the test */
  14. interval(5000).subscribe(() => {
  15. console.log(`All received data: ${array.length}`);
  16. });
  17. async function createServerStreamingServer(): Promise<any> { // '0.0.0.0:3001'
  18. return new Promise((resolve, reject) => {
  19. let server = new grpc.Server()
  20. server.addService(message_proto.MessageService.service, {
  21. HandleMessage: (call) => {
  22. console.log(call.request)
  23. let response = {
  24. id: `1`,
  25. message: 'Hi, I received your request. Thanks for choosing FIS enterprise'
  26. }
  27. call.write(response)
  28. }
  29. })
  30. server.bindAsync(hostServer, grpc.ServerCredentials.createInsecure(), (err, port) => {
  31. assert.ifError(err);
  32. server.start()
  33. resolve(`gRPC server is running on ${hostServer}`)
  34. })
  35. })
  36. }
  37. async function createServerStreamingClient() {
  38. const client = new message_proto.MessageService(targetServer, grpc.credentials.createInsecure());
  39. console.log(`Sending request to ${targetServer} to open response channel...`)
  40. let call = client.HandleMessage({ id: '1', message: `Testing` })
  41. call.on('status', (status: Status) => {
  42. if (status == grpc.status.OK) { // only returns a status when there's error. Otherwise it just waits
  43. console.log(`Message trasmission operation is successful`)
  44. }
  45. if (status == grpc.status.UNAVAILABLE) {
  46. console.log(`Request Failed`)
  47. }
  48. });
  49. call.on('data', (data: any) => {
  50. console.log(data)
  51. });
  52. call.on('error', (err) => {
  53. console.error(err)
  54. });
  55. call.on('end', () => {
  56. console.log(`DONE`)
  57. })
  58. }
  59. createServerStreamingServer().then((res) => {
  60. console.log(res)
  61. createServerStreamingClient()
  62. })
  63. function queryBooks() {
  64. // const queryBooksRequest = new QueryBooksRequest();
  65. // queryBooksRequest.setAuthorPrefix("Geor");
  66. // const client = grpc.client(BookService.QueryBooks, {
  67. // host: host,
  68. // });
  69. // client.onHeaders((headers: grpc.Metadata) => {
  70. // console.log("queryBooks.onHeaders", headers);
  71. // });
  72. // client.onMessage((message: Book) => {
  73. // console.log("queryBooks.onMessage", message.toObject());
  74. // });
  75. // client.onEnd((code: grpc.Code, msg: string, trailers: grpc.Metadata) => {
  76. // console.log("queryBooks.onEnd", code, msg, trailers);
  77. // });
  78. // client.start();
  79. // client.send(queryBooksRequest);
  80. }