-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (36 loc) · 1.16 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
let express = require('express');
let fs = require('fs');
let app = express();
let port = process.env.PORT || 80;
let server = app.listen(port);
app.use(express.static('public'));
let socket = require('socket.io');
console.log("My socket server is running");
let io = socket(server);
io.sockets.on('connection', newConnection);
let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let req = new XMLHttpRequest();
function newConnection(socket) {
console.log('new connection' + socket.id);
socket.on('button', sendRequest);
function sendRequest(data){
console.log(data);
req.open('GET', data.url, false);
req.send(null);
if(req.status == 200) {
io.to(data.id).emit('button', {text: req.responseText, url: data.url, year: data.year, id: data.id});
}
}
socket.on('savedata',saveFile);
function saveFile(data){
console.log(data)
let coursesFormatted = `\n ${data.year} ${data.semester} courses: ${data.courses.toString()}`
fs.appendFile('courses.txt', coursesFormatted, function(err) {
if (err) {
console.log(`Error! Append failed`)
}else {
console.log(`Append successful`)
}
})
}
}