-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
77 lines (67 loc) · 1.96 KB
/
server.ts
File metadata and controls
77 lines (67 loc) · 1.96 KB
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
import { TodoRequest } from "./proto/randomPackage/TodoRequest";
import { TodoResponse } from "./proto/randomPackage/TodoResponse";
import { RandomHandlers } from "./proto/randomPackage/Random";
import path from "path";
import * as grpc from "@grpc/grpc-js";
import * as protoLoader from "@grpc/proto-loader";
import { ProtoGrpcType } from "./proto/random";
const PORT = 8082;
const PROTO_FILE = "./proto/random.proto";
const packageDef = protoLoader.loadSync(path.resolve(__dirname, PROTO_FILE));
// make grpc object
const grpcObject = grpc.loadPackageDefinition(
packageDef
) as unknown as ProtoGrpcType;
const randomPackage = grpcObject.randomPackage;
// initialize server
function main() {
const server = getServer();
// bind async to localhost
server.bindAsync(
`0.0.0.0:${PORT}`,
grpc.ServerCredentials.createInsecure(),
(err, port) => {
if (err) {
console.log(err);
return;
}
console.log(`Your server is running on port: ${port}`);
server.start();
}
);
}
let todoList: TodoResponse = { todos: [] };
function getServer() {
// create server
const server = new grpc.Server();
// define services / Handlers
server.addService(randomPackage.Random.service, {
PingPong: (req, res) => {
console.log(req.request);
res(null, { message: "Pong" });
},
RandomNumbers: (call) => {
const { maxValue = 10 } = call.request;
let runCount = 0;
const id = setInterval(() => {
runCount = ++runCount;
call.write({ number: Math.floor(Math.random() * maxValue) });
if (runCount >= 10) {
clearInterval(id);
call.end();
}
}, 500);
},
TodoList: (call, callback) => {
call.on("data", (chunk: TodoRequest) => {
todoList.todos?.push(chunk);
console.log(chunk);
});
call.on("end", () => {
callback(null, { todos: todoList.todos });
});
},
} as RandomHandlers);
return server;
}
main();