-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (32 loc) · 897 Bytes
/
server.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
var fs = require('fs');
var path = require('path');
const net = require('net')
// Fancy header
var readStream = fs.createReadStream(path.join(__dirname, 'cnsp.txt'), 'utf8');
let data = ''
readStream.on('data', function(chunk) {
data += chunk;
}).on('data', function() {
console.log('\x1b[32m', data);
});
let clients = []
// Start the server
const server = net.createServer(socket => {
console.log('Client connected')
let client_id = clients.length
clients.push({
active: true,
socket
})
socket.on('end', () => {
clients[client_id].active = false
})
socket.on('data', chunk => {
clients
.filter((x, i) => x.active && i !== client_id)
.map(client => client.socket.write(chunk))
})
}).on('error', () => null)
server.listen(3000, () => {
console.log(`Server started on port `, server.address().port)
})