-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 2.02 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
var http = require("http")
var ws = require("nodejs-websocket")
var refreshTime = 30000;
var wsPort = **YOURWSPORT**
//Setup the interval on which we will refresh data from LaundryAlert server
var interval = setInterval(fetchData, refreshTime)
//Used to track when we have no clients online and stop polling LaundryAlert servers when that is the case (avoid suspicious use pattern)
var numClients = 0;
var server = ws.createServer(function(conn) {
console.log("New websocket connection");
conn.on("text", function(text) {
//This whole "hello" and "bye" stuff is a bit of a hack that I had to do because of the way I run this on my server; it's probably not necessary for other users.
//It pretty much serves as a more explicit way to track open connections that represent active users
if(text == "hello") {
numClients++
console.log("connection hello")
} else if(text == "bye") {
numClients--
console.log("Connection bye")
if(numClients == 0) {
console.log("Disabling check due to no clients");
clearInterval(interval)
}
}
});
conn.sendText(lastData)
//If they are the first client to connect in a while, setup the interval so it will update live for the duration of their connection
if(interval._idleTimeout == -1) {
console.log("no interval")
fetchData()
interval = setInterval(fetchData, refreshTime)
}
conn.on("error", function(err) {
console.log("WS error", err);
});
}).listen(wsPort);
function broadcast(textToSend) {
server.connections.forEach(function(conn) {
conn.sendText(textToSend);
});
}
var lastData = "";
function fetchData() {
console.log("fetching data")
var fullData = "";
//This is the URL to fetch from LaundryAlert's system. Adjust it to any other LaundryAlert url of similar format to use with other campuses/locations covered by LaundryAlert.
http.get("http://23.23.147.128/homes/mydata/yellowjacket", function(res) {
res.on("data", function(data) {
fullData += data;
});
res.on("end", function() {
broadcast(fullData)
lastData = fullData
});
})
}