-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.js
165 lines (144 loc) · 4.51 KB
/
chat.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
const { input, renderMessage, init, setConfig, debug, getConfig } = require('./util')
const Channel = require('./message')
const net = require('net')
const fs = require('fs')
const path = require('path')
init();
/**
* accepts incoming socket connection asynchronously
* @param {net.Server} server
* @returns {Promise<net.Socket>}
*/
function accept(server) {
return new Promise((resolve, reject) => {
server.on('connection', socket => {
resolve(socket)
})
})
}
// connects to a socket server asynchronously
function connect(port, host) {
const socket = new net.Socket();
socket.connect(port, host);
return new Promise((resolve, reject) => {
socket.on('connect', function() {
resolve(socket)
})
socket.on('error', function(err) {
reject({err: err})
})
})
}
async function app() {
// ask for initialisation
// console.clear();
console.log("1. Start conversation.");
console.log("2. Join conversation.")
const ans = await input("Enter choice : ")
let socket = new net.Socket();
if (ans == "1") {
// start server and listen for other party to join
// const host = await input("Enter host : ");
var server = net.createServer();
server.listen(5000);
server.on('listening', function () {
console.log('Connection Details ');
console.log('host :', server.address().family, server.address().address);
console.log('port :', server.address().port);
console.log("Waiting for someone to connect...");
});
server.on('error', function (err) {
console.log("error", err);
});
server.on('close', function() {
console.log('server closed')
})
socket = await accept(server);
server.close();
console.log('someone got connected', socket.remoteAddress)
}
else if (ans == "2") {
// join conversation with given information
console.log("Enter details to connect");
const port = 5000 || await input("Port : ");
const host = await input("Host : ");
try {
socket = await connect(port, host);
console.log('connection established');
} catch (error) {
console.log(error)
closeApp();
}
}
else {
process.exit(1);
}
function closeApp() {
if(server) server.close();
socket.destroy()
process.exit(0);
}
socket.on('close', function() {
renderMessage('connection closed')
closeApp();
})
const channel = new Channel(socket);
channel.on('message', function(text) {
renderMessage(`frnd: ${text}`)
})
channel.on('file', file => {
debug(file.metadata, renderMessage);
const fileName = file.metadata.name;
const buffer = file.data;
const filePath = path.resolve(getConfig('DOWNLOAD') || '', fileName);
fs.writeFileSync(filePath, buffer);
renderMessage('file recieved : '+filePath)
})
async function parseCommand(command, arg) {
switch(command) {
case 'file': {
await channel.sendFile(arg);
renderMessage(`me : file sent ${arg}`, false);
break;
}
case 'set': {
debug("set value : ", renderMessage, arg);
const [key, value] = arg.split('=');
setConfig(key, value);
renderMessage(`config set : ${key} = ${value}`, false);
break;
}
case 'get': {
const key = arg.trim();
renderMessage(`${key} : ${getConfig(key)}`, false);
break;
}
default:
renderMessage('command not found', false);
break;
}
}
process.stdout.write('\n');
while (true) {
const res = await input("> ");
if (res == 'exit') {
closeApp();
}
if(res[0] == ":") {
// parse command
const index = res.indexOf(' ');
if(index == -1) {
parseCommand(res.substr(1).trim());
}
else {
parseCommand(res.substring(1, index), res.substring(index+1).trim());
}
}
else {
// send message
channel.sendMessage(res);
renderMessage(`me : ${res}`, false);
}
}
}
app();