-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
350 lines (315 loc) · 9.92 KB
/
Copy pathindex.js
File metadata and controls
350 lines (315 loc) · 9.92 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const http = require('http')
const https = require('https')
const express = require('express')
const cors = require('cors')
const app = express()
const fs = require('fs')
const bodyParser = require('body-parser')
let port = process.env.PORT || 3000
const useSSL = process.env.SSL === 'true' || false
let cert
let key
if (useSSL) {
console.log('Using SSL.')
cert = fs.readFileSync('./cert/certificate.crt')
key = fs.readFileSync('./cert/privateKey.key')
port = 443
}
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
app.use(cors())
app.options('*', cors())
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
let server
if (useSSL) {
let options = {
cert: cert,
key: key
}
server = https.createServer(options, app)
} else {
server = http.createServer(app)
}
server.listen(port)
const getParams = params => {
const map = {}
params.split('&').forEach(item => {
const string = item.split('=')
map[string[0]] = string[1]
})
return map
}
app.get('/', function (request, response) {
response.send('Hello')
})
const InvokeKeys = {
PLAY: 'play',
TIME: 'time',
SELECT: 'select',
CONTROL: 'control',
RESPONSE: 'response'
}
const baseManifest = {
currentTime: 0,
isPlaying: false,
selectedItem: undefined,
currentDriver: undefined,
controller: undefined,
}
const REQUEST_INTERVAL = 2000
let driverActive = false
let map = new Map() // token: manifest
let wsMap = new Map() // token: [{userid, websocket}]
let reqMap = new Map() // token: interval
let playheadMap = new Map() // token: [{userid, number}]
let driverMap = new Map() // token: userid
const WebSocketServer = require('ws').Server
const wss = new WebSocketServer({ server })
console.log('Mock Socket Server running on ' + port + '.')
const assignDriver = (token, userid) => {
driverMap.set(token, userid)
console.log(`${token} has a new driver: ${userid}.`)
}
const clearUpInterval = (token) => {
console.log('clearUpInterval')
if (reqMap.has(token)) {
console.log('CLEAR INTERVAL')
clearInterval(reqMap.get(token).interval)
reqMap.delete(token)
}
}
const setUpInterval = (token) => {
console.log('setupInterval')
clearUpInterval(token)
if (!reqMap.has(token)) {
console.log('SET INTERVAL')
const fn = playheadUpdate(token)
reqMap.set(token, {
interval: setInterval(fn, REQUEST_INTERVAL)
})
fn()
}
}
const playheadUpdate = token => {
return () => {
// if (wsMap.has(token)) {
// wsMap.get(token).forEach(o =>{
// const { ws } = o
// ws.send(JSON.stringify({
// 'request': 'sampleTime'
// }))
// })
// }
if (wsMap.has(token) && driverMap.has(token)) {
const userid = driverMap.get(token)
const socket = wsMap.get(token).find(o => o.userid === userid)
if (socket && !driverActive) {
const { ws } = socket
// console.log(`Go ask driver ${userid} for details in ${token}.`)
ws.send(JSON.stringify({
'request': 'sampleTime'
}))
}
}
}
}
const clearCurrentDriverIf = (token, userid) => {
const manifest = map.get(token)
const { currentDriver } = manifest
if (currentDriver && currentDriver.userid === userid) {
const m = {...manifest, currentDriver: undefined}
map.set(token, m)
update(token, m, userid)
}
}
const update = (token, manifest, fromid) => {
const wsList = wsMap.get(token)
// console.log('UPDATE', token, manifest)
wsList.forEach(obj => {
const { userid, ws } = obj
if (userid !== fromid) {
ws.send(JSON.stringify({
'manifestUpdate': manifest,
}))
}
})
}
const updateDriver = (token, driver, selection, fromid) => {
const wsList = wsMap.get(token)
wsList.forEach(obj => {
const { userid, ws } = obj
if (userid !== fromid) {
ws.send(JSON.stringify({
'driverUpdate': {
driver,
selection
}
}))
}
})
}
const smoothPlayheadTime = (manifest, playheads) => {
const current = manifest.currentTime
const len = playheads.length
const count = playheads.map(o => o.value).reduce((a, b) => a+b, 0)
const smoothed = count / len
if (Math.max(smoothed - current) > 1.5) {
manifest.currentTime = smoothed
}
console.log('SMOOTHED', playheads, current, smoothed)
return manifest
}
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate()
ws.isAlive = false;
ws.ping()
})
}, 30000)
wss.on('connection', (ws, req) => {
console.log('websocket connection open')
const query = req.url.split('?')[1]
if (!query) {
ws.send(JSON.stringify({ error: 'The following query parameters are required: token, userid.' }))
ws.close()
return
}
ws.isAlive = true
ws.on('pong', () => {
ws.isAlive = true
})
const params = getParams(query)
console.log(params)
const { token, userid } = params
if (!wsMap.has(token)) {
console.log(`Create new socket map for ${token}.`)
wsMap.set(token, [])
assignDriver(token, userid)
}
let list = wsMap.get(token)
if (!map.has(token)) {
console.log(`Create new manifest for ${token}.`)
map.set(token, baseManifest)
}
let manifest = map.get(token)
console.log(`Manifest for ${token}:`, JSON.stringify(manifest))
ws.send(JSON.stringify({
'manifestUpdate': manifest
}))
list.forEach(({ userid, ws }) => {
ws.send(JSON.stringify({
'request': 'sampleTime'
}))
})
list.push({ userid, ws })
wsMap.set(token, list)
ws.on('message', message => {
let json = message
if (typeof message === 'string') {
json = JSON.parse(message)
}
console.log('Received: ', JSON.stringify(json, null, 2))
const isCurrentDriver = driverMap.has(token) ? driverMap.get(token) === userid : false
const { type, value, atTime, from } = json
if (atTime) {
manifest.currentTime = atTime
}
if (type === InvokeKeys.PLAY) {
manifest.isPlaying = value
if (!isCurrentDriver) {
assignDriver(token, userid)
manifest.controller = userid
}
if (value) {
setUpInterval(token)
} else {
clearUpInterval(token)
}
} else if (type === InvokeKeys.TIME) {
manifest.currentTime = value
if (!isCurrentDriver) {
assignDriver(token, userid)
manifest.controller = userid
}
} else if (type === InvokeKeys.SELECT) {
manifest.selectedItem = value
if (!isCurrentDriver) {
assignDriver(token, userid)
manifest.controller = userid
}
} else if (type === InvokeKeys.CONTROL) {
manifest.currentDriver = value
if (value && !driverActive) {
assignDriver(token, userid)
driverActive = true
updateDriver(token, value, manifest.selectedItem, from)
manifest.controller = userid
} else if (!value) {
driverActive = false
updateDriver(token, undefined, manifest.selctedItem, from)
playheadUpdate()
}
} else if (type === InvokeKeys.RESPONSE) {
const { request, response } = json
if (request === 'sampleTime' && isCurrentDriver) {
// console.log('RESPONSE', from, response)
// const playheads = playheadMap.has(token) ? playheadMap.get(token) : []
// let index = playheads.findIndex(p => p.userid === userid)
// index = index === -1 ? playheads.length : index
// playheads[index] = {userid, value: response}
// playheadMap.set(token, playheads)
// if (wsMap.get(token).length === 1) {
// return
// }
// manifest = smoothPlayheadTime(manifest, playheads)
manifest.currentTime = response
}
} else {
console.log(`Unhandled manifest change for ${message}`)
}
map.set(token, manifest)
update(token, manifest, from)
})
ws.on('close', () => {
console.log('websocket connection close')
clearCurrentDriverIf(token, userid)
const m = wsMap.get(token)
const p = playheadMap.get(token)
const u = driverMap.get(token)
const driverLeaving = u === userid
let i = m.findIndex(obj => obj.userid === userid)
if (i > -1) {
m.splice(i, 1)
wsMap.set(token, m)
if (m.length === 0) {
clearUpInterval(token)
wsMap.delete(token)
}
}
if (p) {
i = p.findIndex(obj => obj.userid === userid)
if (i > -1) {
p.splice(i, 1)
playheadMap.set(token, p)
}
}
// If the connection leaving was the driver,
// see if we can transfer ownership...
if (driverLeaving) {
console.log(`Driver ${userid} is leaving ${token}.`)
driverMap.delete(token)
if (m.length > 0) {
assignDriver(token, m[0].userid)
}
}
})
})
wss.on('close', function close() {
clearInterval(interval)
})