-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
79 lines (63 loc) · 2.12 KB
/
Copy pathclient.js
File metadata and controls
79 lines (63 loc) · 2.12 KB
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
// Import necessary modules
const grpc = require("@grpc/grpc-js");
const readline = require('readline');
const protoLoader = require("@grpc/proto-loader")
// Load the Protocol Buffer definition file
const packageDef = protoLoader.loadSync("chat.proto", {});
// Load the chat service definition from the Protocol Buffer definition
const grpcObject = grpc.loadPackageDefinition(packageDef);
const chatPackage = grpcObject.chatPackage;
// Read command line argument for text
const text = process.argv[2];
// Create a new gRPC client instance
const client = new chatPackage.Chat("localhost:40000", grpc.credentials.createInsecure());
console.log(text)
// Create a readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Prompt the user for input
console.log("Enter something (or type 'exit' to quit)");
// Function to repeatedly ask for user input
function askUser() {
rl.question('', (answer) => {
// If the user types 'exit', close the readline interface and exit the function
if (answer.toLowerCase() === 'exit') {
rl.close();
return; // Exit the recursive function
}
// Send the user's input as a message
sendMessage(answer);
// Schedule the function to run again after 200 milliseconds
setTimeout(() => {
askUser();
}, 200);
});
}
// Start asking for user input
askUser();
// Event listener for readline close event
rl.on('close', () => {
console.log('Goodbye!');
process.exit(0);
});
// Establish connection to server and set up bidirectional streaming
const call = client.sendCommunication((error, response) => {
if (error) {
console.error('Error:', error);
return;
}
console.log('Received message from server:', response.mess);
});
// Function to send a message to the server
const sendMessage = (content) => {
const message = { mess: content };
call.write(message);
};
// Event listener for incoming messages from the server
call.on('data', (message) => {
console.log('Anonymous:', message.mess);
});
// Event listener for server ending the connection
call.on("end", e => console.log("server done!"));