-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (92 loc) · 2.3 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
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
require('dotenv').config()
const express = require('express')
const http = require('http')
const url = require('url')
const WebSocket = require('ws')
const Axios = require('axios')
const app = express()
const server = http.createServer(app)
const wss = new WebSocket.Server({ server })
const ssoApiClient = Axios.create({
baseURL: 'https://sso.runetek.io/api'
})
const BroadcastSecretKey = process.env.WEBHOOK_KEY
if (!BroadcastSecretKey) {
console.error('Error: WEBHOOK_KEY must be set')
process.exit(1)
}
const fetchCurrentRev = () => ssoApiClient.get('revision').then(({ data }) => data.revision)
class RevisionBroadcaster {
constructor (wss, currentRevision) {
this.wss = wss
this._revision = currentRevision
}
get revision () {
return +this._revision
}
set revision (v) {
if (+v > this.revision) {
this.wss.broadcast(v)
this._revision = v
}
}
}
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data)
}
})
}
function clientHeartbeat (ws) {
try {
if (ws.isAlive === false) {
return ws.terminate()
}
// fail silently
ws.ping('', false, true)
ws.isAlive = false
} catch (e) {
console.error(e)
}
}
const init = async () => {
const revision = await fetchCurrentRev()
const broadcaster = new RevisionBroadcaster(wss, revision)
wss.on('connection', (ws, req) => {
ws.isAlive = true
ws.on('pong', () => {
ws.isAlive = true
})
ws.send(broadcaster.revision)
})
app.get('/', (req, res) => {
fetchCurrentRev().then(rev => {
res.send({
revision: rev
})
}).catch(e => {
res.status(500).send({
error: 'Internal service error occurred'
})
console.error(e)
})
})
setInterval(function () {
wss.clients.forEach(clientHeartbeat)
}, 30e3)
app.post('/broadcast', (req, res) => {
const location = url.parse(req.url, true)
if (location.query.key !== BroadcastSecretKey) {
return res.status(403).send('Not authorized')
}
fetchCurrentRev().then(rev => {
broadcaster.revision = rev
res.send('OK')
})
})
server.listen(8080, function listening() {
console.log('Listening on %d', server.address().port)
})
}
init()