-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
69 lines (62 loc) · 2.74 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import path from 'path'
import * as grpc from '@grpc/grpc-js'
import * as protoLoader from '@grpc/proto-loader'
import { ProtoGrpcType } from "./protos/example"
import { ExampleHandlers } from './protos/examplePackage/Example'
import { TodoResponse } from './protos/examplePackage/TodoResponse'
import { TodoRequest } from './protos/examplePackage/TodoRequest'
const PORT = 8082
const PROTO_FILE = './protos/example.proto'
const packageDef = protoLoader.loadSync(path.resolve(__dirname, PROTO_FILE))
const grpcObj = (grpc.loadPackageDefinition(packageDef) as unknown) as ProtoGrpcType
const examplePackage = grpcObj.examplePackage
// Get gRPC server and bind it to a host/port
const server = getGrpcServer()
server.bindAsync(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
if (err) {
console.error(err)
return
}
console.log(`Server started on port ${port}`)
server.start()
})
const todoList: TodoResponse = {todos: []}
function getGrpcServer() {
const server = new grpc.Server()
server.addService(examplePackage.Example.service, {
// Unary Call. Client sends "Ping" and Server responses with "Pong"
// Check the file client.ts for more details on how client calls this method.
PingPong: (req, res) => {
console.log(req.request)
res(null, {message: "Pong"})
},
// Server Streaming Call. Client sends a number "maxVal" and Server keeps streaming responses with some values a certain number of times.
// Check the file client.ts for more details on how client calls this method with the number.
RandomNumbers: (call) => {
const { maxVal = 10 } = call.request
console.log(maxVal)
let runCount = 0
const intervalId = setInterval(() => {
runCount == ++runCount
call.write({num: Math.floor(Math.random() * maxVal)})
if (runCount >= 10) {
clearInterval(intervalId)
call.end()
}
}, 500)
},
// Client Streaming Call. Client keeps on adding todolists for a certain number of times while server waits for all these request to finish.
// Once the client has finished sending all the values, server will process those.
// Check the file client.ts for more details on how client calls this method with streaming data.
TodoList: (call, callback) => {
call.on("data", (chunk: TodoRequest) => {
console.log(chunk)
todoList.todos?.push(chunk)
})
call.on("end", () => {
callback(null, {todos: todoList.todos})
})
}
} as ExampleHandlers)
return server
}