-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathclient.ts
106 lines (87 loc) · 3.22 KB
/
client.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
import * as debug from "debug";
import * as grpc from "@grpc/grpc-js";
import { Book, GetBookRequest, GetBookViaAuthor } from "./proto/book_pb";
import {BookServiceClient} from "./proto/book_grpc_pb";
const log = debug("SampleClient");
const client = new BookServiceClient("127.0.0.1:50051", grpc.credentials.createInsecure());
const getBook = async (isbn: number) => {
return new Promise((resolve, reject) => {
const request = new GetBookRequest();
request.setIsbn(isbn);
log(`[getBook] Request: ${JSON.stringify(request.toObject())}`);
client.getBook(request, (err, book: Book) => {
if (err != null) {
debug(`[getBook] err:\nerr.message: ${err.message}\nerr.stack:\n${err.stack}`);
reject(err); return;
}
log(`[getBook] Book: ${JSON.stringify(book.toObject())}`);
resolve(book);
});
});
};
const getBooks = () => {
return new Promise((resolve) => {
const stream: grpc.ClientDuplexStream<GetBookRequest, Book> = client.getBooks();
stream.on("data", (data: Book) => {
log(`[getBooks] Book: ${JSON.stringify(data.toObject())}`);
});
stream.on("end", () => {
log("[getBooks] Done.");
resolve();
});
for (let i = 0; i < 10; i++) {
const req = new GetBookRequest();
req.setIsbn(i);
log(`[getBooks] Request: ${JSON.stringify(req.toObject())}`);
stream.write(req);
}
stream.end();
});
};
const getBooksViaAuthor = (author: string) => {
return new Promise((resolve) => {
const request = new GetBookViaAuthor();
request.setAuthor(author);
log(`[getBooksViaAuthor] Request: ${JSON.stringify(request.toObject())}`);
const stream: grpc.ClientReadableStream<Book> = client.getBooksViaAuthor(request);
stream.on("data", (data: Book) => {
log(`[getBooksViaAuthor] Book: ${JSON.stringify(data.toObject())}`);
});
stream.on("end", () => {
log("[getBooksViaAuthor] Done.");
resolve();
});
});
};
const getGreatestBook = () => {
return new Promise((resolve) => {
const stream: grpc.ClientWritableStream<GetBookRequest> = client.getGreatestBook((err, data: Book) => {
if (err != null) {
log(`[getGreatestBook] err:\nerr.message: ${err.message}\nerr.stack:\n${err.stack}`);
}
log(`[getGreatestBook] Book: ${JSON.stringify(data.toObject())}`);
resolve();
});
for (let i = 0; i < 10; i++) {
const req = new GetBookRequest();
req.setIsbn(i);
log(`[getGreatestBook] Request: ${JSON.stringify(req.toObject())}`);
stream.write(req);
}
stream.end();
});
};
async function main() {
await getBook(1);
await getBooks();
await getBooksViaAuthor("DefaultAuthor");
await getGreatestBook();
}
main().then((_) => _);
process.on("uncaughtException", (err) => {
log(`process on uncaughtException error: ${err}`);
});
process.on("unhandledRejection", (err) => {
log(`process on unhandledRejection error: ${err}`);
});