-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
62 lines (53 loc) · 1.48 KB
/
client.js
File metadata and controls
62 lines (53 loc) · 1.48 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
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const { ChatService, printMessage } = require("./client/chatservice");
const PROTO_PATH = "chat.proto";
const SERVER_URI = "0.0.0.0:9090";
const packageDefinition = protoLoader.loadSync(PROTO_PATH);
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const client = new protoDescriptor.ChatService(
SERVER_URI,
grpc.credentials.createInsecure()
);
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const chatservice = new ChatService(client, rl);
function startChat(username) {
try {
chatservice.joinRoom(username);
rl.on("line", function (text) {
text = text.split(":");
if (!chatservice.isBegin && text[0] != "0") {
printMessage("error", "group chat not ready");
return;
}
switch (text[0]) {
case "1":
chatservice.sendMsg(text[1]);
break;
case "2":
chatservice.likeMsg(text[1]);
break;
case "0":
chatservice.outRoom();
break;
default:
printMessage("error", "not correct format");
break;
}
});
} catch (error) {
printMessage("server", "server is down");
process.exit(0);
}
}
// Handle the SIGINT signal
process.on("SIGINT", () => {
chatservice.outRoom();
});
rl.question("What's ur name? ", (answer) => {
startChat(answer);
});