forked from RenXModCommunity/RenXServerViewer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (80 loc) · 2.78 KB
/
app.js
File metadata and controls
98 lines (80 loc) · 2.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const http = require("http");
const fs = require("fs");
var renxdata;
var path = require('path');//helps with file paths
//a helper function to handle HTTP requests
function requestHandler(req, res) {
var
content = '',
fileName = path.basename(req.url),//the file that was requested
localFolder = __dirname + '/public/';//where our public files are located
//NOTE: __dirname returns the root folder that
//this javascript file is in.
if (fileName == '') fileName = 'index.html'
if(fileName){//if index.html was requested...
content = localFolder + fileName;//setup the file name to be returned
//reads the file referenced by 'content'
//and then calls the anonymous function we pass in
fs.readFile(content,function(err,contents){
//if the fileRead was successful...
if(!err){
//send the contents of index.html
//and then close the request
res.end(contents);
} else {
//otherwise, let us inspect the eror
//in the console
console.dir(err);
};
});
} else {
//if the file was not found, set a 404 header...
res.writeHead(404, {'Content-Type': 'text/html'});
//send a custom 'file not found' message
//and then close the request
res.end('<h1>Sorry, the page you are looking for cannot be found.</h1>');
};
};
//step 2) create the server
var server = http.createServer(requestHandler)
function fetchData() {
const https = require("http");
const options = {
hostname: "brotherhoodofnod.net",
port: 8080,
path: "/api/server/?Game%20Version=latest",
method: "GET"
};
var data = "";
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", d => {
data += d;
});
res.on("end", () => {
data = JSON.parse(data);
fs.writeFileSync("./data.json", JSON.stringify(data, null, 2));
});
});
req.on("error", error => {
console.error(error);
});
req.end();
}
fetchData();
setInterval(fetchData, 15000);
setInterval(broadcastToSocket, 5000);
// Loading socket.io
const io = require("socket.io")(server);
// When a client connects, we note it in the console
io.sockets.on("connection", function (socket) {
console.log("Incoming: " + socket.handshake.address);
socket.emit("message", "You are connected!");
renxdata = JSON.parse(fs.readFileSync("data.json", "utf8"));
io.local.emit("renxData", renxdata);
});
function broadcastToSocket() {
renxdata = JSON.parse(fs.readFileSync("data.json", "utf8"));
io.local.emit("renxData", renxdata);
}
server.listen(80);