@@ -35,10 +35,10 @@ function sayHello(ctx) {
3535 metadata . set ( 'indeed' , 'it do' )
3636 // Watcher creates a watch execution context for the watch
3737 // The execution context provides scripts and templates with access to the watch metadata
38- console . log ( "received metadata from client request" , ctx . meta )
39- // console.dir(ctx.metadata, { depth: 3, colors: true });
38+ console . log ( "received metadata from client request" , ctx . metadata )
39+ console . dir ( ctx . metadata , { depth : 3 , colors : true } ) ;
4040 console . log ( `got sayHello request name: ${ ctx . req . name } ` ) ;
41-
41+
4242 // an alias to ctx.response.res
4343 // This is set only in case of DUPLEX calls, to the the gRPC call reference itself
4444 ctx . res = { message : "Hello " + ctx . req . name } ;
@@ -49,7 +49,28 @@ function sayHello(ctx) {
4949
5050 console . log ( `set sayHello response: ${ ctx . res . message } ` ) ;
5151}
52+ // nested Unary stream
53+
54+ function sayHelloNested ( ctx ) {
55+ // create new metadata
56+ let metadata = new grpc . Metadata ( ) ;
57+ metadata . set ( 'it' , 'works?' )
58+ metadata . set ( 'indeed' , 'it do' )
59+ // Watcher creates a watch execution context for the watch
60+ // The execution context provides scripts and templates with access to the watch metadata
61+ console . log ( "received metadata from client request" , ctx . metadata )
62+ console . dir ( ctx . metadata , { depth : 3 , colors : true } ) ;
63+ // console.log("ctx line 64 from server.js", ctx)
64+
65+ // nested unary response call
66+ let firstPerson = ctx . req . firstPerson . name ;
67+ let secondPerson = ctx . req . secondPerson . name ;
68+ console . log ( "firstPerson line 68 from server.js:" , firstPerson )
69+ ctx . res = { "serverMessage" : [ { message : "Hello! " + firstPerson } , { message : 'Hello! ' + secondPerson } ] }
5270
71+ // send response header metadata object directly as an argument and that is set and sent
72+ ctx . sendMetadata ( metadata )
73+ }
5374// Server-Side Stream
5475// used highland library to manage asynchronous data
5576async function sayHellos ( ctx ) {
@@ -64,7 +85,9 @@ async function sayHellos(ctx) {
6485
6586 // alias for ctx.request.req
6687 // In case of UNARY and RESPONSE_STREAM calls it is simply the gRPC call's request
88+
6789 let reqMessages = { "message" : 'hello!!! ' + ctx . req . name }
90+
6891 dataStream . push ( reqMessages )
6992 reqMessages = dataStream
7093 let streamData = await hl ( reqMessages )
@@ -80,6 +103,7 @@ async function sayHellos(ctx) {
80103 ctx . res . end ( )
81104}
82105
106+
83107// Client-Side stream
84108function sayHelloCs ( ctx ) {
85109 // create new metadata
@@ -146,7 +170,7 @@ function sayHelloBidi(ctx) {
146170 */
147171function main ( ) {
148172 const app = new Mali ( PROTO_PATH , "Greeter" ) ;
149- app . use ( { sayHello, sayHellos, sayHelloCs, sayHelloBidi } ) ;
173+ app . use ( { sayHello, sayHelloNested , sayHellos, sayHelloCs, sayHelloBidi } ) ;
150174 app . start ( HOSTPORT ) ;
151175 console . log ( `Greeter service running @ ${ HOSTPORT } ` ) ;
152176}
0 commit comments