-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (84 loc) · 2.52 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const grpc = require('grpc')
const notesProto = grpc.load('./protos/notes.proto')
const notes = [
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'}
]
const server = new grpc.Server()
server.addService(notesProto.NoteService.service, {
list: (call, callback) => {
console.log(call)
console.log(callback)
callback(null, notes);
},
getStream: (call) => {
// console.log("\n ========== CALL START\n", call)
noteStream = {
notes: notes,
order: "-1"
}
for (let i = 0; i < 3; i++) {
noteStream.order = ""+i
call.write(noteStream);
}
// console.log("\n ========== CALL IN PROGRESS\n", call)
call.end()
// console.log("\n ========== CALL END\n", call)
},
putStream: (call, callback) => {
call.on('data', (notes_stream) => {
// process data
console.log(notes_stream)
})
call.on('end', () => {
// process end
console.log("\n ========== streaming end\n")
callback(false, {
code: 0,
msg: "successfully streamed to server"
})
})
call.on('error', (error) => {
// process error
console.log("\n ========== error\n", error)
callback(true, {
code: 1,
msg: error
})
})
call.on('status', (status) => {
// process status
console.log("\n ========== status\n", status)
})
},
getPutStream: (call) => {
call.on('data', (debug_message) => {
// process data
console.log(debug_message.msg)
})
call.on('error', (error) => {
// process error
console.log("\n ========== error\n", error)
})
call.on('end', () => {
// process end
console.log("\n ========== streaming end\n")
})
let i = 0;
var refId = null;
function sendDataToServer(){
i += 1
call.write({
msg: "Server is sending "+i
});
if(i == 7){
call.end()
clearInterval(refId)
}
}
refId = setInterval(sendDataToServer, 1000)
}
});
server.bind('127.0.0.1:50051', grpc.ServerCredentials.createInsecure())
console.log('Server running at http://127.0.0.1:50051')
server.start()