This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (87 loc) · 2.42 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//importing libaries
global.fetch = require('sync-fetch');
global.express = require('express');
global.http = require('http');
global.socketio = require('socket.io');
global.fs = require('fs');
//initing libaries
global.app = express();
global.server = http.createServer(app);
global.io = socketio(server);
//importing files
global.api = require('./api');
global.settings = require('./settings.json');
//express seving site
app.use(express.static('public'));
server.listen(settings.port, () => { console.log(`>Listenin on port ${settings.port}`) });
//init api
api.init();
//count users
let usercount = 0;
fs.writeFile(settings.userLogFile, '', ()=>{});
//logging
function log(data, file=null) {
console.log(data);
if (file) {
fs.appendFile(file, `${data}\n`, ()=>{});
}
}
//socket server
io.on('connection', (socket) => {
//count users
usercount++;
let id = usercount;
log(`>user #${id} connected`, file=settings.userLogFile);
//disconnect
socket.on('disconnect', () => {
log(`>user #${id} disconnected`, file=settings.userLogFile);
});
//institutes request
socket.on('institutes', (callback) => {
//request
let institutes = api.institutes();
//error
if (!institutes) {
socket.emit('error', 'institutes');
return;
}
//reply
callback(institutes);
});
//login request
socket.on('login', (credentials, callback) => {
//request
let token = api.token(credentials);
//error
if (!token) {
socket.emit('error', 'login');
return;
}
//reply
callback(token);
});
//evaluations request
socket.on('evaluations', (token, callback) => {
//get data
let evaluations = api.evaluations(token);
//error
if (!evaluations) {
socket.emit('error', 'evaluations');
return;
}
//reply
callback(evaluations);
});
//timestable request
socket.on('timetable', (token, weekOff, callback) => {
//get data
let timetable = api.timetable(token, weekOff);
//error
if (!timetable) {
socket.emit('error', 'timetable');
return;
}
//reply
callback(timetable);
});
});