-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
86 lines (71 loc) · 2.3 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
83
84
85
86
var WebSocket = require('ws');
var cheerio = require('cheerio');
var rp = require('request-promise');
var mysql = require('mysql');
var Promise = require("bluebird");
//wow i forgot that you could do this xD
var conf = require("./config.json");
var connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
database: conf.database
});
connection.connect();
//fet the current websocket url from the DOM of reddit
function getSocketURL(){
return new Promise(function(resolve, reject){
rp('https://reddit.com/place')
.then(function(body){
var $ = cheerio.load(body);
wsurl = JSON.parse($('#config').text().slice(0,-1).substring(8)).place_websocket_url;
resolve(wsurl)
});
});
}
//check if the message fits our criteria
function isValidMessage(message){
return (message.x || message.x === 0) && (message.y || message.y === 0) && message.author && (message.color || message.color === 0);
}
//global for keeping track of frams over time
var incomingFrames = 0;
//keep this global so we dont keep making new websocket instances
var ws;
//regisiter the
function createSocketHandler(){
//ensure that the object is dead before we create another ws instance
if(ws !== null){
ws = null;
delete ws;
}
getSocketURL()
.then( (wsurl) => {
ws = new WebSocket(wsurl);
console.log("Connecting to " + wsurl);
ws.on('message', (input) => {
var d = new Date();
//console.log(input);
var message = JSON.parse(input).payload;
if(isValidMessage(message)) {
var query = "INSERT INTO place (x, y, username, colour, time) VALUES (" + message.x + "," + message.y + ",'" + message.author + "'," + message.color + ",'" + d.getTime() + "');";
connection.query(query);
}
incomingFrames++;
});
ws.on("close", () => {
console.log("We lost connection since the server was destroyed, let's reconnect :)");
createSocketHandler();
});
ws.on("open", () => {
setInterval(() => {
if(incomingFrames <= 0){
console.log("We havent received packets within 5 seconds, not possible server is broken.")
createSocketHandler();
}
incommingFrames = 0;
}, 5000);
});
});
}
//create the initial socket connection
createSocketHandler();