-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
55 lines (45 loc) · 1.45 KB
/
server.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
import path from 'path';
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { ProtoGrpcType } from './proto/random';
import { RandomHandlers } from './proto/randomPackage/Random';
const PORT = 8082;
const PROTO_FILE = './proto/random.proto';
const packageDefinition = protoLoader.loadSync(path.resolve(__dirname, PROTO_FILE));
const grpcObject = grpc.loadPackageDefinition(packageDefinition) as unknown as ProtoGrpcType;
const randomPackage = grpcObject.randomPackage;
function main() {
const server = getServer();
server.bindAsync(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure(),
(err, port) => {
if (err != null) {
console.error(err);
return;
}
server.start();
console.log(`gRPC server running at ${port}`);
}
);
}
function getServer() {
const server = new grpc.Server();
server.addService(randomPackage.Random.service, {
"PingPong": (req, res) => {
console.log("Request: ", req);
res(null, { message: 'pong' });
},
"RandomNumbers": (call) => {
const { maxValue = 100, count = 10 } = call.request;
const interval = setInterval(() => {
const value = Math.floor(Math.random() * maxValue);
call.write({value});
}, 1000);
setTimeout(() => {
clearInterval(interval);
call.end();
}, count * 1000); // count seconds
},
} as RandomHandlers);
return server;
}
main();