-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (82 loc) · 2.8 KB
/
index.js
File metadata and controls
99 lines (82 loc) · 2.8 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
99
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger-output.json');
const config = require('./config/config');
const connectDB = require('./config/connectdb.js');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
require('dotenv').config({
path: `.env.${process.env.NODE_ENV || 'development'}`
});
connectDB();
const PORT = config.port || 3000;
app.use(cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204
}));
app.use(express.json());
let currentOTP = null;
let finalTime = 1;
let currentId = null;
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
wss.on('connection', (ws) => {
ws.on('message', (message) => {
const data = JSON.parse(message);
if (data.type === 'time_update') {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'time_update2', time: data.time, location: data.location, range:data.range }));
}
});
}else if(data.type === 'attendance'){
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'attendance2', rollNumber: data.rollNumber }));
}
});
}else if(data.type === 'teacherLoc'){
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'teacherLoc', location: data.location, range:data.range }));
}
});
}
if(data.type==='first_call'){
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'first_call' }));
}
});
}
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
app.post('/setAttendance', (req, res) => {
const { otp, time, id } = req.body;
currentOTP = otp;
finalTime = time;
currentId = id;
res.send('OTP and Final Time set');
});
app.get('/getAttendance', (req, res) => {
res.json({currentOTP, finalTime, currentId});
});
app.get('/', (req, res) => {
res.send('Server is running...')
});
app.use("/api/student", require("./routes/student.routes.js"));
app.use("/api/teacher", require("./routes/teacher.routes.js"));
app.use("/api/superadmin", require("./routes/superadmin.routes.js"));
app.use("/api/class", require("./routes/class.routes.js"));
app.use("/api/attendance", require("./routes/attendance.routes.js"));
server.listen(PORT,"0.0.0.0", () => {
console.log(`Server started on port ${PORT} and Environment is ${config.env}`);
});