forked from decipherhub/MSM-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_interval.ts
63 lines (56 loc) · 1.83 KB
/
client_interval.ts
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
import * as grpc from "@grpc/grpc-js";
import * as protoLoader from "@grpc/proto-loader";
import path from "path";
// Define the path to your .proto file
const PROTO_PATH = path.resolve(__dirname, "computation.proto");
// Load the .proto file
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
// Assume your package is named "computation"
const computation = protoDescriptor.computation as any;
// Create a client for the ComputationService
const client = new computation.ComputationService(
"localhost:50051",
grpc.credentials.createInsecure()
);
// Function to call sendComputationData
const sendComputationData = () => {
const requestData = { data: "Computation Data #2" };
client.sendComputationData(
requestData,
(error: grpc.ServiceError | null, response: any) => {
if (error) {
console.error(`Error calling sendComputationData: ${error.message}`);
} else {
console.log(
`Received from sendComputationData: ${JSON.stringify(response)}`
);
}
}
);
};
// Function to call getComputationResult
const getComputationResult = () => {
const requestResult = { result: "Your computation result data here" };
client.getComputationResult(
requestResult,
(error: grpc.ServiceError | null, response: any) => {
if (error) {
console.error(`Error calling getComputationResult: ${error.message}`);
} else {
console.log(
`Received from getComputationResult: ${JSON.stringify(response)}`
);
}
}
);
};
// Schedule sendComputationData and getComputationResult to be called every 5 seconds
setInterval(sendComputationData, 5000);
setInterval(getComputationResult, 5000);