-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (100 loc) · 3.31 KB
/
server.js
File metadata and controls
131 lines (100 loc) · 3.31 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
const net = require('net')
const host = 'localhost'
const port = 8000
// Create and return a net.Server object, the function will be invoked when client connect to this server.
const server = net.createServer()
// Make the server a TCP server listening on port 9999.
server.listen({host,port}, function () {
const serverInfo = server.address()
console.log(`TCP server is listening on : ${serverInfo.address}:${serverInfo.port}`)
server.on('close', function () {
console.log('TCP server socket is closed.')
})
server.on('error', function (error) {
console.error(JSON.stringify(error))
})
})
const connectedClients = []
server.on('connection', (client) => {
connectedClients.push(client)
console.log(`Local Address : ${client.localAddress}:${client.localPort}`)
console.log(`Remote Address : ${client.remoteAddress}:${client.remotePort}`)
console.log('Connected!')
client.setEncoding('utf-8')
var logged_in = false
client.on('data', (data) => {
data = data.replace(/(\r\n|\n|\r)/gm,"")
console.log(`${client.remoteAddress}:${client.remotePort} : ${data}`)
var arg = data.split(' ')
if(arg[0] === 'login' && arg[1] === 'kaung'){
console.log(`${client.remoteAddress}:${client.remotePort} : just logged in`)
logged_in = true
}
if(logged_in){
switch(arg[0]){
case 'list':
var msg = ''
for (var i = 0; i < connectedClients.length ; i++) {
msg += JSON.stringify(connectedClients[i]._peername) + ' writable: ' + connectedClients[i].writable + '\n'
}
client.write(msg)
break
case 'send':
var msg = ''
for (var i = 2; i < arg.length; i++) {
msg += arg[i] + ' '
}
var index = parseInt(arg[1])
if(index < connectedClients.length ){
if(connectedClients[index].writable){
connectedClients[index].write(msg)
}
else{
client.write('Cannot send! This socket has been closed.')
}
}
break
case 'show':
console.log(connectedClients[arg[1]])
break
}
}
else {
client.write('Please Login!')
}
})
client.on('end', () => {
console.log('Client disconnect.')
server.getConnections(function (err, count) {
if(!err)
{
console.log("There are %d connections now. ", count)
}else
{
console.error(JSON.stringify(err))
}
})
})
// When client timeout.
client.on('timeout', function () {
console.log('Client request time out. ')
client.end()
})
})
// const standard_input = process.stdin
// standard_input.setEncoding('utf-8')
// standard_input.on('data', function (data) {
// if(data === 'exit\n'){
// process.exit()
// }else
// {
// var argv = data.split(' ')
// if(argv.length > 1){
// var index = parseInt(argv[0])
// if(connectedClients.length > index){
// console.log('Sending to ' + connectedClients[index]._peername)
// connectedClients[index].write(data)
// }
// }
// }
// })