-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverHandler.js
48 lines (36 loc) · 1.16 KB
/
serverHandler.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
'use strict';
const { encrypt, decrypt } = require('./encryptText');
const readline = require('readline');
const rl = readline.createInterface({input: process.stdin, output: process.stdout, prompt: "you: "});
const onData = data =>
{
rl.setPrompt("client: ");
rl.write('\n');
process.stdout.write(decrypt(data.toString().replace(/\r?\n|\r/g, "")));
rl.setPrompt("you: ");
rl.write('\n');
}
const serverHandler = socket =>
{
console.log('client connected:', socket.remoteAddress);
rl.prompt();
process.stdin.resume();
//socket.setEncoding('utf8');
//receive
socket.on('data', onData);
//send
const onLine = input =>
{
if(input.match(/^\s*$/))
{
readline.clearLine(process.stdout, 0); // clear current text
readline.cursorTo(process.stdout, 0); // move cursor to beginning of line
}
rl.prompt();
if(input.trim() != "")
socket.write(encrypt(input));
}
rl.on('line', onLine);
socket.on('error', error => { console.log(error.code); });
}
module.exports = serverHandler;