-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (39 loc) · 1.16 KB
/
server.js
File metadata and controls
48 lines (39 loc) · 1.16 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
const net = require('net'),
clients = new Set(),
HOST = '127.0.0.1',
PORT = 30000,
{createInterface} = require('readline'),
cmd = createInterface({
input: process.stdin,
output: process.stdout
})
clients.send = function (data, client) {
for (let c of this) {
if (client && c !== client) c.write(data)
}
}
cmd.on('line', msg => {
clients.send(`[SERVER] ${msg}`, true)
})
const server = net.createServer(async client => {
clients.add(client)
console.log(`On: ${client.remoteAddress}:${client.remotePort}`)
clients.send(`${client.remotePort} Entrou.`, client)
client.on('data', async data => {
console.log(`[${client.remoteAddress}]: ${data}`)
if (data.toString().startsWith('/s ')) return
clients.send(`[${client.remotePort}] ${data}`, client)
})
client.on('close', async data => {
clients.delete(client)
clients.send(`${client.remotePort} Saiu.`, true)
console.log(`Off: ${client.remoteAddress}:${client.remotePort}`)
})
client.on('error', err => {
//console.log('Erro:', err)
client.destroy()
})
//client.pipe(client)
})
server.listen(PORT, HOST)
process.title = `[Server] ${HOST}:${PORT}`