-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathweb.js
More file actions
73 lines (60 loc) · 1.91 KB
/
Copy pathweb.js
File metadata and controls
73 lines (60 loc) · 1.91 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
const fs = require('fs');
const express = require('express');
const app = express();
const hbs = require('express-handlebars');
const robots = require('express-robots');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const net = require('net');
const os = require('os');
const uniqid = require('uniqid');
"use strict";
app.engine('hbs', hbs({extname: 'hbs'}));
app.set('views', (__dirname + '/views'));
app.set('view engine', 'hbs');
app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/public', express.static(__dirname + '/public'));
app.use(robots({UserAgent: '*', Disallow: '/'}));
app.get('/', function (req, res) {
res.render('index', {url: req.get('host')});
});
app.use(function(req, res) {
res.status(404);
res.send('404 error :(');
});
const clients = new Map();
io.on('connection', socket => {
socket.client_id = uniqid();
socket.filter = {};
clients.set(uniqid, socket);
socket.on('choose_filter', data => {
if(data.hasOwnProperty('period')) {
socket.filter = {'lowest_at': parseFloat(data['period'])};
}
console.log(data);
});
socket.on('disconnect', () => {
clients.delete(socket.client_id);
});
});
server.listen(3001);
const socket_path = __dirname + '/pipe.sock';
let socket_client = net.createConnection(socket_path);
socket_client.on('error', err => {
console.log(err);
}).on('data', data => {
data.toString().split(os.EOL).forEach(s => {
if(s === '') {
return;
}
let arr = JSON.parse(s);
console.log(arr);
for(let client of clients.values()) {
if(client.filter.hasOwnProperty('lowest_at')
&& arr[5].indexOf(client.filter['lowest_at']) !== -1) {
client.emit('alert', arr);
}
}
});
});