-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTlRpcServer.js
59 lines (53 loc) · 1.39 KB
/
TlRpcServer.js
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
const TlRpc = require("./TlRpc")
const grpc = require('@grpc/grpc-js');
/**
* rpc通用server
* @version 1.0.0
* @author iamtsm
*/
class TlRpcServer extends TlRpc{
/**
* 初始化
* @param {*} rpcConfig
*/
constructor(rpcConfig) {
super(rpcConfig)
}
/**
* -- 对外提供
* 获取实例化service接口列表对象
* @returns service instance
*/
#serviceDefinition() {
const instance = this.implInstance();
if (!instance) {
return null;
}
return instance.service;
}
/**
* 启动服务
* 启动时需要添加相应的启动函数
* @returns
*/
async startServer(handlerMap = {}){
if(Object.keys(handlerMap).length === 0){
return null;
}
console.log(handlerMap)
const server = new grpc.Server();
const config = this.getConfig();
return await new Promise(resolve => {
server.addService(this.#serviceDefinition(), handlerMap);
server.bindAsync(
config.ip + ":" + config.port,
grpc.ServerCredentials.createInsecure(), () => {
server.start();
console.log(`${JSON.stringify(config)} server start!`)
resolve(handlerMap)
}
);
})
}
}
module.exports = TlRpcServer;