-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
73 lines (63 loc) · 2.15 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
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync(
__dirname + '/protobuf/test.proto',
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const test_proto = protoDescriptor.test;
const testSimpleSimple = function (call, callback) {
console.log('Server: Simple Message Received = ', call.request); // Server: Simple Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
callback(null, call.request);
};
const testStreamSimple = function (call, callback) {
var messages = [];
call.on('data', function (m) {
console.log('Server: Stream Message Received = ', m); // Server: Stream Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
messages.push(m);
});
call.on('end', function () {
callback(null, messages.pop());
});
};
const testSimpleStream = function (call) {
console.log('Server: Simple Message Received = ', call.request); // Server: Simple Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
call.write(call.request);
call.write(call.request);
call.end();
};
const testStreamStream = function (call) {
call.on('data', function (message) {
console.log('Server: Stream Message Received = ', message); // Server: Stream Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
setTimeout(function () {
call.write({
id: message.id // IMPORTANT only for Bidirectional Stream Request
});
}, 10);
});
call.on('end', function () {
call.end();
});
};
main = function () {
server = new grpc.Server();
server.addService(test_proto.Test.service, {
testSimpleSimple: testSimpleSimple,
testStreamStream: testStreamStream,
testStreamSimple: testStreamSimple,
testSimpleStream: testSimpleStream
});
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
server.start();
}
main();