forked from timetocode/master-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
97 lines (85 loc) · 2.67 KB
/
Copy pathServer.js
File metadata and controls
97 lines (85 loc) · 2.67 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
var TCPServer = require('./TCPServer')
var HTTPServer = require('./HTTPServer')
var protectedProperties = ['id', 'authenticated', 'password']
function Server(options) {
this.clients = {}
this.tcpPassword = options.tcpPassword
this.tcpServer = new TCPServer(options.ip, options.tcpPort,
// tcp on connect
(client) => {
this.connect(client)
},
// tcp on message
(id, message) => {
this.message(id, message)
},
// tcp on disconnect
(id) => {
this.close(id)
}
)
this.httpServer = new HTTPServer(options.ip, options.httpPort,
// http
() => {
return JSON.stringify(this.getServerListing())
}
)
this.cachedListing = []
this.cacheTimestamp = Date.now()
this.cacheDuration = options.cacheDuration || 500
}
Server.prototype.start = function() {
this.tcpServer.start()
this.httpServer.start()
}
Server.prototype.connect = function(client) {
this.clients[client.id] = client
client.authenticated = false
}
Server.prototype.message = function(id, message) {
var client = this.clients[id]
for (var prop in message) {
// if a tcpPassword is defined, all messages except password will be
// ignored until the correct password is received
if (typeof this.tcpPassword !== 'undefined' && !client.authenticated) {
if (prop === 'password') {
if (message[prop] === this.tcpPassword) {
client.authenticated = true
}
}
} else {
// if authenticated (or not using a password), accept everything
// except the protected properties
if (protectedProperties.indexOf(prop) === -1) {
client[prop] = message[prop]
}
}
}
}
Server.prototype.close = function(id) {
delete this.clients[id]
}
Server.prototype.getServerListing = function() {
var now = Date.now()
if (now > this.cacheTimestamp + this.cacheDuration) {
// rebuild cache
this.cachedListing = this.buildServerList()
this.cacheTimestamp = now
}
return this.cachedListing
}
Server.prototype.buildServerList = function() {
var arr = []
for (var id in this.clients) {
var client = this.clients[id]
var listing = {}
for (var prop in client) {
if (protectedProperties.indexOf(prop) === -1) {
listing[prop] = client[prop]
}
}
arr.push(listing)
}
return arr
}
module.exports = Server