Skip to content

Commit

Permalink
All time intervals are now configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pdonald committed Dec 22, 2016
1 parent 91b0b70 commit ab92145
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 13 additions & 5 deletions config.js
Expand Up @@ -4,12 +4,20 @@ let isProd = process.env.NODE_ENV === 'production'

module.exports = {
isProd: isProd,
auth: { user: 'user', password: 'pass' },
port: 8080,
wshost: 'ws://localhost:8080',
wsdashboard: 'ws://localhost:8080/dashboard',
trackDashboard: false,
anonymize: false,
analytics: {
wsurl: 'ws://localhost:8080',
anonymize: false,
updateInterval: 20000,
timingUpdateDelay: 20000,
consoleCountInterval: isProd ? 60000 : 10000
},
dashboard: {
wsurl: 'ws://localhost:8080/dashboard',
auth: { user: 'user', password: 'pass' },
broadcastInterval: 1000,
trackSelf: false,
},
webpack: {
entry: ['./dashboard.jsx', !isProd && 'webpack-hot-middleware/client'].filter(x=>x),
output: { path: '/' },
Expand Down
18 changes: 9 additions & 9 deletions server.js
Expand Up @@ -16,7 +16,7 @@ let users = {}
let userCount = 0
let userLastID = 0

setInterval(() => console.log(`Users online: ${userCount}`), 10 * 1000)
setInterval(() => console.log(`Users online: ${userCount}`), config.analytics.consoleCountInterval)

wss.on('connection', socket => {
userCount++
Expand All @@ -25,7 +25,7 @@ wss.on('connection', socket => {
let ip = socket.upgradeReq.headers['x-real-ip'] || socket.upgradeReq.connection.remoteAddress
let user = users[id] = {
id: id,
ip: !config.anonymize ? ip : null,
ip: !config.analytics.anonymize ? ip : null,
ipgeo: geoip.lookup(ip),
ua: useragent.lookup(socket.upgradeReq.headers['user-agent']).toJSON(),
date: Date.now(),
Expand Down Expand Up @@ -66,7 +66,7 @@ wss.on('error', err => console.error(err))

app.get('/analytics.js', (req, res) => {
let trackerjs = `
var socket = new WebSocket('${config.wshost}');
var socket = new WebSocket('${config.analytics.wsurl}');
socket.onopen = function() {
socket.send(JSON.stringify({
type: 'init',
Expand All @@ -81,15 +81,15 @@ app.get('/analytics.js', (req, res) => {
scroll: 100.0*document.documentElement.scrollTop/document.documentElement.scrollHeight,
focus: 'hidden' in document ? !document.hidden : undefined,
}));
}, 20000);
}, ${config.analytics.updateInterval});
setTimeout(function() {
if (socket.readyState != socket.OPEN) return;
socket.send(JSON.stringify({
type: 'timing',
timing: window.performance ? window.performance.timing.toJSON() : null
}));
}, 10000);
}, ${config.analytics.timingUpdateDelay});
};`

res.set('Content-Type', 'application/javascript')
Expand All @@ -116,7 +116,7 @@ app.get('/test/*', (req, res) => {
function isAuth(req) {
try {
let header = req.headers.authorization
let userpass = config.auth.user + ':' + config.auth.password
let userpass = config.dashboard.auth.user + ':' + config.dashboard.auth.password
return header && header.indexOf('Basic ') === 0 &&
new Buffer(header.split(' ')[1], 'base64').toString() === userpass
} catch (e) {
Expand Down Expand Up @@ -157,9 +157,9 @@ app.get('/', basicAuth, (req, res) => {
</head>
<body>
<div id="root"></div>
<script>window.config = { wsdashboard: '${config.wsdashboard}' };</script>
<script>window.config = { wsdashboard: '${config.dashboard.wsurl}' };</script>
<script src="bundle.js"></script>
${config.trackDashboard && '<script src="analytics.js"></script>'}
${config.dashboard.trackSelf && '<script src="analytics.js"></script>'}
</body>
</html>
`
Expand All @@ -182,4 +182,4 @@ wssadmin.on('connection', socket => {
sendData()
})

setInterval(() => wssadmin.clients.forEach(s => sendData(s)), 1000)
setInterval(() => wssadmin.clients.forEach(s => sendData(s)), config.dashboard.broadcastInterval)

0 comments on commit ab92145

Please sign in to comment.