-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (40 loc) · 1.39 KB
/
index.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
const grpc = require('grpc')
const googleProtobufAnyPb = require('google-protobuf/google/protobuf/any_pb.js')
const path = require('path')
function Client ({ip, packageName, protoMsg, servicePath, messagesPath}) {
const services = require(servicePath)
const messages = require(messagesPath)
this.client = new services.ReportServiceClient(ip, grpc.credentials.createInsecure())
this.messages = messages
this.packageName = packageName
this.protoMsg = protoMsg
}
Client.prototype.grpcSend = function grpcSend(data) {
const {messages, packageName, protoMsg} = this
const request = new messages.Request()
const typeUrlAny = `${packageName}.${protoMsg}`
const requestAny = new messages[protoMsg]()
const kyesAny = requestAny.toObject()
const anyPb = new googleProtobufAnyPb.Any()
Object.keys(data).forEach(key => {
// 目前proto中extra为google.protobuf.Any类型
if (key === 'extra') {
Object.keys(data.extra).forEach(keyAny => {
if (keyAny in kyesAny) {
requestAny[`set${upperCase(keyAny)}`](data.extra[keyAny])
}
})
anyPb.pack(requestAny.serializeBinary(), typeUrlAny)
} else {
// 枚举传对应的数字标识
request[`set${upperCase(key)}`](data[key])
}
})
request.setExtra(anyPb)
this.client.report(request, (err, res) => {
if (err) {
console.log('grpc error', err)
}
})
}
module.exports = Client