mirror of https://github.com/grpc/grpc-web.git
				
				
				
			internal sync
This commit is contained in:
		
							parent
							
								
									c1b07a70d6
								
							
						
					
					
						commit
						4ca20a4f9c
					
				|  | @ -16,12 +16,12 @@ | |||
|  * | ||||
|  */ | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <google/protobuf/compiler/code_generator.h> | ||||
| #include <google/protobuf/compiler/plugin.h> | ||||
| #include <google/protobuf/descriptor.h> | ||||
| #include <google/protobuf/io/printer.h> | ||||
| #include <google/protobuf/io/zero_copy_stream.h> | ||||
| #include <algorithm> | ||||
| 
 | ||||
| using google::protobuf::Descriptor; | ||||
| using google::protobuf::FieldDescriptor; | ||||
|  | @ -740,12 +740,14 @@ void PrintServerStreamingCall(Printer* printer, std::map<string, string> vars) { | |||
|       " * @return {!grpc.web.ClientReadableStream<!proto.$out$>}\n" | ||||
|       " *     The XHR Node Readable Stream\n" | ||||
|       " */\n" | ||||
|       "proto.$package_dot$$service_name$$client_type$.prototype.$js_method_name$ =\n"); | ||||
|       "proto.$package_dot$$service_name$$client_type$.prototype." | ||||
|       "$js_method_name$ =\n"); | ||||
|   printer->Indent(); | ||||
|   if (vars["client_type"] == "PromiseClient") { | ||||
|     printer->Print( | ||||
|         "  function(request, metadata) {\n" | ||||
|         "return this.delegateClient_.client_.serverStreaming(this.delegateClient_.hostname_ +\n"); | ||||
|         "return this.delegateClient_.client_.serverStreaming(" | ||||
|         "this.delegateClient_.hostname_ +\n"); | ||||
|   } else { | ||||
|     printer->Print( | ||||
|         "  function(request, metadata) {\n" | ||||
|  |  | |||
|  | @ -16,12 +16,16 @@ syntax = "proto3"; | |||
| 
 | ||||
| package grpc.gateway.testing; | ||||
| 
 | ||||
| message Empty { | ||||
| } | ||||
| 
 | ||||
| message EchoRequest { | ||||
|   string message = 1; | ||||
| } | ||||
| 
 | ||||
| message EchoResponse { | ||||
|   string message = 1; | ||||
|   int32 message_count = 2; | ||||
| } | ||||
| 
 | ||||
| // Request type for server side streaming echo. | ||||
|  | @ -44,6 +48,18 @@ message ServerStreamingEchoResponse { | |||
|   string message = 1; | ||||
| } | ||||
| 
 | ||||
| // Request type for client side streaming echo. | ||||
| message ClientStreamingEchoRequest { | ||||
|   // A special value "" indicates that there's no further messages. | ||||
|   string message = 1; | ||||
| } | ||||
| 
 | ||||
| // Response type for client side streaming echo. | ||||
| message ClientStreamingEchoResponse { | ||||
|   // Total number of client messages that have been received. | ||||
|   int32 message_count = 1; | ||||
| } | ||||
| 
 | ||||
| // A simple echo service. | ||||
| service EchoService { | ||||
|   // One request followed by one response | ||||
|  | @ -54,8 +70,34 @@ service EchoService { | |||
|   rpc EchoAbort(EchoRequest) returns (EchoResponse) { | ||||
|   }; | ||||
| 
 | ||||
|   // One empty request, ZERO processing, followed by one empty response | ||||
|   // (minimum effort to do message serialization). | ||||
|   rpc NoOp(Empty) returns (Empty); | ||||
| 
 | ||||
|   // One request followed by a sequence of responses (streamed download). | ||||
|   // The server will return the same client message repeatedly. | ||||
|   rpc ServerStreamingEcho(ServerStreamingEchoRequest) | ||||
|       returns (stream ServerStreamingEchoResponse); | ||||
| 
 | ||||
|   // One request followed by a sequence of responses (streamed download). | ||||
|   // The server abort directly. | ||||
|   rpc ServerStreamingEchoAbort(ServerStreamingEchoRequest) | ||||
|       returns (stream ServerStreamingEchoResponse) { | ||||
|   } | ||||
| 
 | ||||
|   // A sequence of requests followed by one response (streamed upload). | ||||
|   // The server returns the total number of messages as the result. | ||||
|   rpc ClientStreamingEcho(stream ClientStreamingEchoRequest) | ||||
|       returns (ClientStreamingEchoResponse); | ||||
| 
 | ||||
|   // A sequence of requests with each message echoed by the server immediately. | ||||
|   // The server returns the same client messages in order. | ||||
|   // E.g. this is how the speech API works. | ||||
|   rpc FullDuplexEcho(stream EchoRequest) returns (stream EchoResponse); | ||||
| 
 | ||||
|   // A sequence of requests followed by a sequence of responses. | ||||
|   // The server buffers all the client messages and then returns the same | ||||
|   // client messages one by one after the client half-closes the stream. | ||||
|   // This is how an image recognition API may work. | ||||
|   rpc HalfDuplexEcho(stream EchoRequest) returns (stream EchoResponse); | ||||
| } | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ using grpc::Status; | |||
| using grpc::gateway::testing::EchoRequest; | ||||
| using grpc::gateway::testing::EchoResponse; | ||||
| using grpc::gateway::testing::EchoService; | ||||
| using grpc::gateway::testing::Empty; | ||||
| using grpc::gateway::testing::ServerStreamingEchoRequest; | ||||
| using grpc::gateway::testing::ServerStreamingEchoResponse; | ||||
| 
 | ||||
|  | @ -67,6 +68,12 @@ Status EchoServiceImpl::EchoAbort(ServerContext* context, | |||
|                 "Aborted from server side."); | ||||
| } | ||||
| 
 | ||||
| Status EchoServiceImpl::NoOp(ServerContext* context, const Empty* request, | ||||
|                              Empty* response) { | ||||
|   CopyClientMetadataToResponse(context); | ||||
|   return Status::OK; | ||||
| } | ||||
| 
 | ||||
| Status EchoServiceImpl::ServerStreamingEcho( | ||||
|     ServerContext* context, const ServerStreamingEchoRequest* request, | ||||
|     ServerWriter<ServerStreamingEchoResponse>* writer) { | ||||
|  | @ -82,3 +89,14 @@ Status EchoServiceImpl::ServerStreamingEcho( | |||
|   } | ||||
|   return Status::OK; | ||||
| } | ||||
| 
 | ||||
| Status EchoServiceImpl::ServerStreamingEchoAbort( | ||||
|     ServerContext* context, const ServerStreamingEchoRequest* request, | ||||
|     ServerWriter<ServerStreamingEchoResponse>* writer) { | ||||
|   CopyClientMetadataToResponse(context); | ||||
|   ServerStreamingEchoResponse response; | ||||
|   response.set_message(request->message()); | ||||
|   writer->Write(response); | ||||
|   return Status(grpc::StatusCode::ABORTED, | ||||
|                 "Aborted from server side."); | ||||
| } | ||||
|  |  | |||
|  | @ -40,11 +40,20 @@ class EchoServiceImpl final : | |||
|       grpc::ServerContext* context, | ||||
|       const grpc::gateway::testing::EchoRequest* request, | ||||
|       grpc::gateway::testing::EchoResponse* response) override; | ||||
|   grpc::Status NoOp( | ||||
|       grpc::ServerContext* context, | ||||
|       const grpc::gateway::testing::Empty* request, | ||||
|       grpc::gateway::testing::Empty* response) override; | ||||
|   grpc::Status ServerStreamingEcho( | ||||
|       grpc::ServerContext* context, | ||||
|       const grpc::gateway::testing::ServerStreamingEchoRequest* request, | ||||
|       grpc::ServerWriter< | ||||
|       grpc::gateway::testing::ServerStreamingEchoResponse>* writer) override; | ||||
|   grpc::Status ServerStreamingEchoAbort( | ||||
|       grpc::ServerContext* context, | ||||
|       const grpc::gateway::testing::ServerStreamingEchoRequest* request, | ||||
|       grpc::ServerWriter< | ||||
|       grpc::gateway::testing::ServerStreamingEchoResponse>* writer) override; | ||||
| }; | ||||
| 
 | ||||
| #endif  // NET_GRPC_GATEWAY_EXAMPLES_ECHO_ECHO_SERVICE_IMPL_H_
 | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| /* | ||||
|  * | ||||
|  * Copyright 2015 gRPC authors. | ||||
|  * Copyright 2018 Google LLC | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  | @ -33,7 +33,10 @@ var packageDefinition = protoLoader.loadSync( | |||
| var protoDescriptor = grpc.loadPackageDefinition(packageDefinition); | ||||
| var echo = protoDescriptor.grpc.gateway.testing; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  * @return {!Object} metadata | ||||
|  */ | ||||
| function copyMetadata(call) { | ||||
|   var metadata = call.metadata.getMap(); | ||||
|   var response_metadata = new grpc.Metadata(); | ||||
|  | @ -43,12 +46,20 @@ function copyMetadata(call) { | |||
|   return response_metadata; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function doEcho(call, callback) { | ||||
|   callback(null, { | ||||
|     message: call.request.message | ||||
|   }, copyMetadata(call)); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function doEchoAbort(call, callback) { | ||||
|   callback({ | ||||
|     code: grpc.status.ABORTED, | ||||
|  | @ -56,6 +67,9 @@ function doEchoAbort(call, callback) { | |||
|   }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  */ | ||||
| function doServerStreamingEcho(call) { | ||||
|   var senders = []; | ||||
|   function sender(message, interval) { | ||||
|  | @ -77,7 +91,7 @@ function doServerStreamingEcho(call) { | |||
| /** | ||||
|  * Get a new server with the handler functions in this file bound to the methods | ||||
|  * it serves. | ||||
|  * @return {Server} The new server object | ||||
|  * @return {!Server} The new server object | ||||
|  */ | ||||
| function getServer() { | ||||
|   var server = new grpc.Server(); | ||||
|  |  | |||
|  | @ -254,9 +254,9 @@ in the current directory: | |||
| These are also the 2 files that our `client.js` file imported earlier in the | ||||
| example. | ||||
| 
 | ||||
| ## Compile the Client Javascript Code | ||||
| ## Compile the Client JavaScript Code | ||||
| 
 | ||||
| Next, we need to compile the client side Javascript code into something that | ||||
| Next, we need to compile the client side JavaScript code into something that | ||||
| can be consumed by the browser. | ||||
| 
 | ||||
| ```sh | ||||
|  |  | |||
|  | @ -17,8 +17,11 @@ syntax = "proto3"; | |||
| package helloworld; | ||||
| 
 | ||||
| service Greeter { | ||||
|   // unary call | ||||
|   rpc SayHello (HelloRequest) returns (HelloReply); | ||||
|   // server streaming call | ||||
|   rpc SayRepeatHello (RepeatHelloRequest) returns (stream HelloReply); | ||||
|   // unary call - response after a length delay | ||||
|   rpc SayHelloAfterDelay (HelloRequest) returns (HelloReply); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -11,11 +11,14 @@ var packageDefinition = protoLoader.loadSync( | |||
|      defaults: true, | ||||
|      oneofs: true | ||||
|     }); | ||||
| var protoDescriptor = grpc.loadPackageDefinition(packageDefinition) | ||||
| var protoDescriptor = grpc.loadPackageDefinition(packageDefinition); | ||||
| var helloworld = protoDescriptor.helloworld; | ||||
| var client = new helloworld.Greeter('localhost:9090', | ||||
|                                     grpc.credentials.createInsecure()); | ||||
| 
 | ||||
| /** | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function runSayHello(callback) { | ||||
|   client.sayHello({name: 'John'}, {}, (err, response) => { | ||||
|     console.log(response.message); | ||||
|  | @ -23,6 +26,9 @@ function runSayHello(callback) { | |||
|   }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function runSayRepeatHello(callback) { | ||||
|   var stream = client.sayRepeatHello({name: 'John', count: 5}, {}); | ||||
|   stream.on('data', (response) => { | ||||
|  | @ -33,6 +39,9 @@ function runSayRepeatHello(callback) { | |||
|   }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function runSayHelloAfterDelay(callback) { | ||||
|   var deadline = new Date(); | ||||
|   deadline.setSeconds(deadline.getSeconds() + 1); | ||||
|  |  | |||
|  | @ -33,10 +33,17 @@ var packageDefinition = protoLoader.loadSync( | |||
| var protoDescriptor = grpc.loadPackageDefinition(packageDefinition); | ||||
| var helloworld = protoDescriptor.helloworld; | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function doSayHello(call, callback) { | ||||
|   callback(null, {message: 'Hello! '+ call.request.name}); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  */ | ||||
| function doSayRepeatHello(call) { | ||||
|   var senders = []; | ||||
|   function sender(name) { | ||||
|  | @ -55,6 +62,10 @@ function doSayRepeatHello(call) { | |||
|   }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @param {!Object} call | ||||
|  * @param {function():?} callback | ||||
|  */ | ||||
| function doSayHelloAfterDelay(call, callback) { | ||||
|   function dummy() { | ||||
|     return (cb) => { | ||||
|  | @ -68,6 +79,9 @@ function doSayHelloAfterDelay(call, callback) { | |||
|   }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * @return {!Object} gRPC server | ||||
|  */ | ||||
| function getServer() { | ||||
|   var server = new grpc.Server(); | ||||
|   server.addProtoService(helloworld.Greeter.service, { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue