-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcontrol.server.js
257 lines (217 loc) · 8.02 KB
/
webcontrol.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class WebControlServer {
constructor (server, durationInHours = 1) {
this.io = require('socket.io')(server)
this.screenClients = []
this.controllerClients = []
this.sessionDurationInHours = durationInHours
this.io.on('connection', function (socket) {
socket.on('linkController', linkController)
socket.on('alreadyLinked', alreadyLinked)
socket.on('getSpecialNumber', createScreenSession)
socket.on('data', processData)
socket.on('unpair', unpair)
})
const linkController = (specialNumber, socketId, sessionId) => {
this.linkController(specialNumber, socketId, sessionId)
}
const alreadyLinked = (specialNumber, socketId, sessionId) => {
this.alreadyLinked(specialNumber, socketId, sessionId)
}
const createScreenSession = (socketId, storedSpecialNumber) => {
this.createScreenSession(socketId, storedSpecialNumber)
}
const processData = (data, originSocketId, specialNumber) => {
this.processData(data, originSocketId, specialNumber)
}
const unpair = (specialNumber, socketId) => {
this.unpair(specialNumber, socketId)
}
}
url (url, specialNumber) {
const destinationSocketId = this.getClientBySpecialNumber(specialNumber).id
this.io.to(`${destinationSocketId}`).emit('urlRedirect', url)
}
processData (data, originSocketId, specialNumber) {
if (this.screenHas(specialNumber) && this.isUrl(data)) {
const destinationSocketId = this.getClientBySpecialNumber(specialNumber).id
this.io.to(`${destinationSocketId}`).emit('urlRedirect', data)
} else {
this.io.to(`${originSocketId}`).emit('data', 'error')
}
}
isUrl (value) {
// must include https:// or http
// eslint-disable-next-line
var expression = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/gi
var regex = new RegExp(expression)
return value.match(regex)
}
alreadyLinked (specialNumber, socketId, sessionId) {
if (this.screenHas(specialNumber) && this.findController(specialNumber, sessionId)) {
this.updateController(specialNumber, socketId, sessionId)
this.io.to(`${socketId}`).emit('alreadyLinked', true)
} else {
this.io.to(`${socketId}`).emit('alreadyLinked', false)
}
}
linkController (specialNumber, socketId, sessionId) {
if (this.screenHas(specialNumber) && !this.controllerHas(specialNumber)) {
const clientSocketId = this.getClient(specialNumber)
this.storeControllerClient(specialNumber, socketId, clientSocketId, sessionId)
this.io.to(`${socketId}`).emit('linkController', {
clientSocketId: clientSocketId,
specialNumber: specialNumber
})
} else {
this.io.to(`${socketId}`).emit('linkController', 'error')
}
}
createScreenSession (socketId, storedSpecialNumber) {
const clientWithSpecialNumber = this.getClientBySpecialNumber(storedSpecialNumber)
if (!clientWithSpecialNumber || this.invalidSession(storedSpecialNumber)) {
// new screen client
this.removeInvalidClient(storedSpecialNumber)
this.invalidateControllerSession(storedSpecialNumber)
this.createClient(socketId)
} else {
// client already exists
let client = this.getClientBySpecialNumber(storedSpecialNumber)
// updates client socketId
client = this.updateControlId(client, 'id', socketId)
// sends data to the client
this.sendData(client, socketId)
}
}
storeControllerClient (specialNumber, controllerClientSocketId, clientSocketId, sessionId) {
const newController = {
controllerSocketId: controllerClientSocketId,
clientSocketId: clientSocketId,
specialNumber: specialNumber,
sessionId: sessionId
}
this.controllerClients.push(newController)
}
// Generic helper functions
isIncluded (attribute, value, collection) {
return collection.some((obj) => {
return obj[attribute] === value
})
}
findBy (attribute, value, collection) {
return collection.find((obj) => {
return obj[attribute] === value
})
}
removeBy (collection, attribute, value) {
return collection.filter(obj => obj[attribute] !== value)
}
//
screenHas (specialNumber) {
return this.isIncluded('specialNumber', specialNumber, this.screenClients)
}
controllerHas (specialNumber) {
return this.isIncluded('specialNumber', specialNumber, this.controllerClients)
}
clientExist (socketId) {
return this.isIncluded('id', socketId, this.screenClients)
}
getClient (specialNumber) {
const client = this.findBy('specialNumber', specialNumber, this.screenClients)
return client.id
}
getClientById (socketId) {
return this.findBy('id', socketId, this.screenClients)
}
getClientBySpecialNumber (specialNumber) {
return this.findBy('specialNumber', specialNumber, this.screenClients)
}
getControllerBySpecialNumber (specialNumber) {
return this.findBy('specialNumber', specialNumber, this.controllerClients)
}
getUnusedRandom () {
let aRandomNumber = this.getRandom(1000, 9999)
const repited = (element) => element.specialNumber === aRandomNumber
let unusedRandom = this.screenClients.some(repited)
while (unusedRandom) {
aRandomNumber = this.getRandom(1000, 9999)
unusedRandom = this.screenClients.some(repited)
}
return aRandomNumber
}
getRandom (min, max) {
return Math.floor(Math.random() * (max - min)) + min
}
sendData (client, socketId) {
// sending specialNumber to screen client
this.io.to(`${socketId}`).emit('getSpecialNumber', client)
}
sessionExpired (sessionTimestamp) {
const timeSpanInSeconds = (Date.now() - sessionTimestamp) / 1000
const timeSpanInHours = timeSpanInSeconds / 3600
return timeSpanInHours > this.sessionDurationInHours
}
invalidSession (storedSpecialNumber) {
let isValid = false
const client = this.getClientBySpecialNumber(storedSpecialNumber)
if (client) {
isValid = this.sessionExpired(client.timestamp)
}
return isValid
}
createClient (socketId) {
// create new client
const client = {
id: socketId,
specialNumber: this.getUnusedRandom(),
timestamp: Date.now()
}
this.screenClients.push(client)
this.sendData(client, socketId)
}
// TODO: refactor as removeInvalidScreen
removeInvalidClient (storedSpecialNumber) {
if (this.invalidSession(storedSpecialNumber)) {
this.screenClients = this.screenClients.filter(client => client.specialNumber !== storedSpecialNumber)
}
}
removeInvalidController (storedSpecialNumber) {
if (this.invalidSession(storedSpecialNumber)) {
this.controllerClients = this.controllerClients.filter(controller => controller.specialNumber !== storedSpecialNumber)
}
}
updateControlId (control, id, socketId) {
control[id] = socketId
return control
}
unpair (specialNumber, socketId) {
this.unpairControllerClient(this.getControllerBySpecialNumber(specialNumber),
specialNumber)
// controllerSocketId
this.io.to(`${socketId}`).emit('unpairController')
}
unpairControllerClient (client, specialNumber) {
if (client) {
var clients = this.removeBy(this.controllerClients, 'specialNumber', specialNumber)
this.controllerClients = clients
}
}
invalidateControllerSession (specialNumber) {
const controller = this.getControllerBySpecialNumber(specialNumber)
if (controller) {
this.alreadyLinked(controller.specialNumber, controller.controllerSocketId, controller.sessionId)
this.removeInvalidController(specialNumber)
}
}
findController (specialNumber, sessionId) {
return this.controllerClients.find((controller) => {
return controller.specialNumber === specialNumber && controller.sessionId === sessionId
})
}
updateController (specialNumber, socketId, sessionId) {
var controller = this.findController(specialNumber, sessionId)
if (controller) {
controller.controllerSocketId = socketId
}
}
}
module.exports = WebControlServer