-
Notifications
You must be signed in to change notification settings - Fork 1
/
macosClient.js
67 lines (59 loc) · 1.47 KB
/
macosClient.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
60
61
62
63
64
65
66
67
const socket = require('socket.io-client')('http://localhost:5050')
const grpc = require('@grpc/grpc-js')
const protoLoader = require('@grpc/proto-loader')
let client = null
const initGrpc = (argv) => {
console.log(`initGrpc argv2 = ${argv}`)
const PROTO_PATH = __dirname + '/helloworld.proto'
const packageDefinition = protoLoader.loadSync(
PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
})
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition)
const helloworld = protoDescriptor.helloworld
const host = argv[0]
client = new helloworld.Greeter(`${host}:50051`,
grpc.credentials.createInsecure())
}
const procGrpc = (socket, argv) => {
console.log(`argv = ${argv}`)
client.sayHelloAgain({
name: "dummy"
}, (err, response) => {
if (!err) {
const html = `${response.message}`
socket.emit('gopher front', '1000')
socket.emit('gopher sendHtml', html)
}
})
}
socket.on('disconnect', () => {})
socket.on('connect', () => {
let count = 2
//console.log('connect!')
socket.on('gopher recv', (msg) => {
console.log(`recv: ${msg}`)
count--
if (count <= 0) {
process.exit(0)
}
})
setTimeout(() => {
if (argv.length > 0) {
procGrpc(socket, argv.join(' '))
}
}, 1)
})
const argv = []
for (const i in process.argv) {
if (i < 2) {
continue
}
argv.push(process.argv[i])
}
//console.log(argv)
initGrpc(argv)