You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.2 KiB
141 lines
5.2 KiB
const http = require('http')
|
|
const url = require('url')
|
|
const fs = require('fs')
|
|
const ws = require('ws')
|
|
const redis = require('redis')
|
|
const mime = require('mime')
|
|
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics']
|
|
|
|
//redis client
|
|
const redis_cli = redis.createClient({
|
|
host: 'redis',
|
|
port: 6379
|
|
})
|
|
redis_cli.on("error", function (error) {
|
|
console.error(error)
|
|
})
|
|
|
|
//HTTP server
|
|
http.createServer(function (req, res) {
|
|
const q = url.parse(req.url, true);
|
|
let filename = "./public" + q.pathname;
|
|
let path_split = q.pathname.split("/", 4);
|
|
if (path_split[path_split.length - 1] === "") filename += "/index.html";
|
|
let file_type = mime.getType(filename)
|
|
if (path_split[1] === "connect" && path_split[2] !== "") { //device registration
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
redis_cli.hmset("device;" + path_split[3], "status", "online", "ip", req.connection.remoteAddress, "time", Date.now())
|
|
wss.clients.forEach(clients => {
|
|
clients.send("device;"+path_split[3]+";online")
|
|
})
|
|
console.log(`device;${path_split[3]} just connected`)
|
|
res.write("200");
|
|
return res.end();
|
|
}
|
|
fs.readFile(filename, function(err, data) {
|
|
if (err) {
|
|
res.writeHead(404, {'Content-Type': "text/html"});
|
|
return res.end("404 Not Found"+ req.connection.remoteAddress);
|
|
}
|
|
res.writeHead(200, {'Content-Type': file_type});
|
|
res.write(data);
|
|
return res.end();
|
|
})
|
|
}).listen(8080);
|
|
|
|
//WS server
|
|
const wss = new ws.Server({
|
|
port: 8081,
|
|
perMessageDeflate: {
|
|
zlibDeflateOptions: {
|
|
// See zlib defaults.
|
|
chunkSize: 1024,
|
|
memLevel: 7,
|
|
level: 3
|
|
},
|
|
zlibInflateOptions: {
|
|
chunkSize: 10 * 1024
|
|
},
|
|
// Other options settable:
|
|
clientNoContextTakeover: true, // Defaults to negotiated value.
|
|
serverNoContextTakeover: true, // Defaults to negotiated value.
|
|
serverMaxWindowBits: 10, // Defaults to negotiated value.
|
|
// Below options specified as default values.
|
|
concurrencyLimit: 10, // Limits zlib concurrency for perf.
|
|
threshold: 1024 // Size (in bytes) below which messages
|
|
// should not be compressed.
|
|
}
|
|
});
|
|
|
|
//check available devices
|
|
let check_devices = setInterval(to => {
|
|
redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err)
|
|
if(keys){ keys.forEach(key => {
|
|
redis_cli.hgetall(key, function(err, device) { if(err) console.log(err)
|
|
if (device){
|
|
let device_available = "online"
|
|
let xhr = new XMLHttpRequest();
|
|
xhr.open('GET', `http://[${device.ip}]/info`, true);
|
|
xhr.send();
|
|
setTimeout(to => {
|
|
if (xhr.readyState !== 4){
|
|
device_available = "offline"
|
|
xhr.abort()
|
|
}
|
|
if (device_available !== device.status){
|
|
wss.clients.forEach(clients => clients.send(`${key};${device_available}`))
|
|
redis_cli.hmset(key, "status", device_available, "ip", device.ip, "time", Date.now())
|
|
console.log(`${key} is now ${device_available}`)
|
|
}
|
|
}, 2000)
|
|
}
|
|
})
|
|
})}
|
|
})
|
|
}, 5000)
|
|
///*
|
|
//WS handler
|
|
wss.on('connection', (ws, req) => {
|
|
ws.on('message', message => {
|
|
console.log(`${req.socket.remoteAddress} => ${message}`)
|
|
let msg = message.split(";", 3)
|
|
if (msg[0] === "device") {
|
|
redis_cli.hget(`device;${msg[1]}`, "ip", function(err, ip) {
|
|
if(err) console.log(err)
|
|
if(ip){
|
|
let xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function() {
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
wss.clients.forEach(clients => {
|
|
clients.send(message)
|
|
})
|
|
}
|
|
}
|
|
xhr.open('GET', `http://[${ip}]/${msg[2]}`, true);
|
|
xhr.send();
|
|
setTimeout(to => {if (xhr.readyState !== 4){
|
|
ws.send(`error;device "${msg[1]}" took too long to reach`)
|
|
ws.send(`device;${msg[1]};offline`)
|
|
xhr.abort()
|
|
}}, 1500)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
ws.send(`connected as ${req.socket.remoteAddress}`)
|
|
console.log(`${req.socket.remoteAddress} connected`)
|
|
//on new connection
|
|
redis_cli.keys("device;*", function(err, keys) {
|
|
if (err) console.log(err)
|
|
if (keys) { keys.forEach(key => {
|
|
redis_cli.hget(key, "status", function (err, status) {
|
|
if (err) console.log(err)
|
|
if (status) ws.send(`${key};${status}`)
|
|
})
|
|
})}
|
|
})
|
|
})//*/
|