-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (138 loc) · 3.55 KB
/
Copy pathserver.js
File metadata and controls
152 lines (138 loc) · 3.55 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const net = require("net");
const readline = require ("readline")
const { IpAddress, port } = require("./config");
const option = { host: IpAddress.IpAddress, port: port.port };
let getConn = connName => {
let client = net.createConnection(option, () => {
console.log("Connection name : " + connName);
console.log(
"Connection local address : " +
client.localAddress +
":" +
client.localPort
);
console.log(
"Connection remote address : " +
client.remoteAddress +
":" +
client.remotePort
);
});
client.setEncoding("utf8");
client.on("data", (data) => {
console.log("\n............\nGot Data!: ", data, '\n............\n');
try {
let parsed = data.split('\n')
for(let json of parsed){
if(json !== ''){
let incomingData = JSON.parse(json);
if(incomingData.type === 'msg'){ //checks to see if there is only a message attribute
try{
if (incomingData.msg.time) {
if(incomingData.msg.random > 30){
console.log('Random number is greater than 30: ', incomingData.msg.random)
}
}
}catch(err){
console.log('Could not parse time object: ', err)
}
}
}
}
} catch (err) {
console.error("*******ERROR******* \nJSON object not formatted properly");
}
});
client.on("end", function() {
console.log("Client socket disconnect. ");
});
client.on("error", function(err) {
console.error('An error has occurred: ', JSON.stringify(err));
client.end()
});
return client;
}
const listenForCommand = (rl)=>{
rl.prompt();
return new Promise((res,rej) => {
rl.once('line', (input) => {
input = input.toLowerCase();
switch( input ){
case 'time':
res ({'request': 'time'});
console.log('Asking for Server Time');
break;
case 'count':
res({'request': 'count'});
console.log('Asking for Count');
break;
case 'exit':
rej("exit");
break;
default:
try{
let parse = JSON.parse(input)
console.log('valid json input')
res(parse)
}catch(e){
res("bad");
}
}
rl.once('close',()=>rej('closed'));
});
});
}
const login = (client,name) => {
client.write(JSON.stringify({ name }));
}
const connect = (name) => {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.setPrompt('Enter a command');
let client = getConn("Node");
client.setTimeout(2000);
client.once("connect", () => {
login(client,name);
mainLoop(rl,client);
});
client.once("timeout", function() {
console.log("Connection timeout.");
client.end();
rl.close();
return connect(name);
});
};
const getLogin=()=> {
return new Promise((res,rej)=>{
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Login: ', (name)=> {
if(name.length > 12){
rl.close();
rej('Usernames cannot be longer than 12 characters');
} else {
rl.close();
res(name)
};
});
});
}
const mainLoop=(rl,client) => {
listenForCommand(rl).then(data=>{
if (!client.destroyed) {
if (data!=='bad')
client.write(JSON.stringify(data));
mainLoop(rl,client);
}
}).catch((e)=>{
console.error("*** Catch in mainloop.....")
})
}
getLogin().then(name=>{
connect(name);
});