-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (65 loc) · 2.06 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
const fs = require('fs');
const server = require('http').createServer();
const io = require('socket.io')(server);
require('dotenv').config();
const SCREENLOGS_LOCATION = process.env.SCREENLOGS_LOCATION;
/*
* Room names
*/
const getHomeRoom = (home) => `home-${home}`;
/*
* Socket.IO
*/
io.on('connection', socket => {
console.log('Client connected');
socket.on('disconnect', reason => {
});
socket.on('subscribe-home', home => {
console.log(`Subscription request for home ${home}`);
// TODO: sanitize home name
let room = getHomeRoom(home);
socket.join(room);
socket.emit('logs', 'Connected');
});
});
server.listen(3000, () => {
console.log('Listening...');
});
let trackings = {};
fs.watch(SCREENLOGS_LOCATION, (type, file) => {
let stat = undefined;
try {
stat = fs.statSync(SCREENLOGS_LOCATION + file);
} catch (e) {
console.log('Error while stating', file, e);
return;
}
let newSize = stat.size;
let match = file.match(/screenlog\.OGP_(HOME|UPDATE)_0*(\d*)/);
if (!match || match.length !== 3) return;
let id = match[2];
let logType = match[1];
let lastRead = getLastReadSize(id, logType, newSize - 100);
if (newSize !== lastRead) {
fs.createReadStream(SCREENLOGS_LOCATION + file, {start: lastRead})
.on('data', (data) => {
console.log('Sending', data.toString(), 'from', id, 'event type:', logType.toLowerCase());
io.to(getHomeRoom(id)).emit(logType.toLowerCase(), data.toString());
});
setLastReadSize(id, logType, newSize);
}
});
function getLastReadSize(id, logType, defaultValue) {
if (!trackings[id])
return defaultValue;
if (!trackings[id][logType])
return defaultValue;
return trackings[id][logType]['lastRead'];
}
function setLastReadSize(id, logType, newSize) {
if (!trackings[id])
trackings[id] = {};
if (!trackings[id][logType])
trackings[id][logType] = {};
trackings[id][logType]['lastRead'] = newSize;
}