-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.94 KB
/
index.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
const express = require('express')
const app = express()
const WebSocket = require('ws')
const { privileges, users } = require('./app/stub')
const {
utils: {
throwError,
handleInvalidJSON,
handleInvalidData,
handleUnknownMethod,
getRandomInt
}
} = require('./app')
const appPort = 3000
const socketPort = 3001
// Serve our css, js
app.use(express.static(`${__dirname}/public`))
// Send index.html by / route
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`)
})
// Run app and listen appPort port
app.listen(appPort, initWebSockets)
function initWebSockets() {
// Run ws server
const WS = new WebSocket.Server({ port: socketPort })
WS.on('connection', socket => {
// After connection subscribe on message even
socket.on('message', data => {
// try to parse JSON data
try {
data = JSON.parse(data)
} catch (e) {
return handleInvalidJSON(socket)
}
// extract data
const { method, id = null, params = {} } = data
// you can pass additional params and use additional conditions here
console.log('request params: ', params)
try {
// we always need id, because this is our marker
if (!id) throwError('id is required', 400)
// create an artificial server delay from 500 to 1250, you can change this limits
const timeout = getRandomInt(500, 1250)
// artificial server delay
setTimeout(() => {
// method router
switch(method) {
case 'getUsers':
return socket.send(JSON.stringify({ id, data: users }))
case 'getPrivileges':
return socket.send(JSON.stringify({ id, data: privileges }))
default:
handleUnknownMethod(method, id, socket)
}
}, timeout)
} catch (err) {
// handle all errors and send to frontend
handleInvalidData(err, id, socket)
}
})
})
}