Skip to content

Commit

Permalink
Support for server streaming (#27)
Browse files Browse the repository at this point in the history
* Add a streaming example to the server

* implement a sreaming call in the client

* Update the inject to intercept streaming callbacks

* Method type icons for stream and unary

* Only display data that is available

* Remove dummy data used for develoment
  • Loading branch information
rogchap authored May 11, 2019
1 parent 4872892 commit 9d78d62
Show file tree
Hide file tree
Showing 15 changed files with 876 additions and 113 deletions.
19 changes: 15 additions & 4 deletions example/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Sentencer from 'sentencer';
import { ExampleTwoRequest } from './example2_pb';
import { ExampleServicePromiseClient, ExampleServiceClient } from './example_grpc_web_pb';
import { ExampleOneRequest } from './example_pb';
import { ExampleOneRequest, StreamRequest } from './example_pb';

const __DEV__ = true;
const enableDevTools = window.__GRPCWEB_DEVTOOLS__ || (() => { });
Expand All @@ -23,7 +23,7 @@ function exampleOne() {
const req = new ExampleOneRequest();
req.setMsg(Sentencer.make("This is {{ an_adjective }} {{ noun }}."));
client.exampleOne(req).then(res => {
document.body.innerHTML += `<div>${res.getMsg()}</div>`
document.body.innerHTML += `<div>${res.getMsg()}</div>`;
}).catch(console.error)
}

Expand All @@ -33,15 +33,26 @@ function exampleTwo() {
if (err) {
return console.error(err);
}
document.body.innerHTML += `<div>${res.getMsg()}</div>`
document.body.innerHTML += `<div>${res.getMsg()}</div>`;
})
}

function alwaysError() {
client.alwaysError(new ExampleOneRequest()).catch(() => { })
}

function exampleStream(isErr) {
var req = new StreamRequest();
req.setError(isErr);
const stream = client.streamingExample(req);
stream.on('data', res => {
document.body.innerHTML += `<div>${res.getTime()}</div>`;
})
}

exampleOne()
exampleStream()
exampleStream(true)
setInterval(exampleOne, 8000)
setInterval(exampleTwo, 10000)
setInterval(alwaysError, 15000)
setInterval(alwaysError, 15000)
50 changes: 50 additions & 0 deletions example/client/example_grpc_web_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,55 @@ proto.s12.example.ExampleServicePromiseClient.prototype.alwaysError =
};


/**
* @const
* @type {!grpc.web.AbstractClientBase.MethodInfo<
* !proto.s12.example.StreamRequest,
* !proto.s12.example.ServerTime>}
*/
const methodInfo_ExampleService_StreamingExample = new grpc.web.AbstractClientBase.MethodInfo(
proto.s12.example.ServerTime,
/** @param {!proto.s12.example.StreamRequest} request */
function(request) {
return request.serializeBinary();
},
proto.s12.example.ServerTime.deserializeBinary
);


/**
* @param {!proto.s12.example.StreamRequest} request The request proto
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!grpc.web.ClientReadableStream<!proto.s12.example.ServerTime>}
* The XHR Node Readable Stream
*/
proto.s12.example.ExampleServiceClient.prototype.streamingExample =
function(request, metadata) {
return this.client_.serverStreaming(this.hostname_ +
'/s12.example.ExampleService/StreamingExample',
request,
metadata || {},
methodInfo_ExampleService_StreamingExample);
};


/**
* @param {!proto.s12.example.StreamRequest} request The request proto
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!grpc.web.ClientReadableStream<!proto.s12.example.ServerTime>}
* The XHR Node Readable Stream
*/
proto.s12.example.ExampleServicePromiseClient.prototype.streamingExample =
function(request, metadata) {
return this.client_.serverStreaming(this.hostname_ +
'/s12.example.ExampleService/StreamingExample',
request,
metadata || {},
methodInfo_ExampleService_StreamingExample);
};


module.exports = proto.s12.example;

Loading

0 comments on commit 9d78d62

Please sign in to comment.