-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
99 lines (82 loc) · 2.66 KB
/
socket.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
const { Socket } = require('dgram')
require('dotenv').config()
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server, {
cors: { origin : "*" }
})
// let clients = []
app.use(express.json());
app.post('/alert', (req, res) => {
console.log('Alert received.')
if(req.body.SECRET_KEY == process.env.SECRET_KEY){
switch (req.body.apps) {
case 'recruitment':
io.sockets.emit("getRecruitmentAlert", req.body.message);
break;
case 'hr':
io.sockets.emit("getHrAlert", req.body.message);
break;
default:
break;
}
res.send('Alert sent.')
} else {
res.send('ERROR!')
}
})
// eventsHandler = (req, res) => {
// const headers = {
// 'Content-Type': 'text/event-stream',
// 'Connection': 'keep-alive',
// 'Cache-Control': 'no-cache'
// };
// res.writeHead(200, headers);
// const clientId = Date.now();
// const newClient = {
// id: clientId,
// res
// };
// clients.push(newClient);
// req.on('close', () => {
// clients = clients.filter(client => client.id !== clientId);
// });
// }
// sendEventsToAll = (newAlert) => {
// clients.forEach(client => client.res.write(`data: ${JSON.stringify(newAlert)}\n\n`))
// }
// async function triggerEvent(req, res){
// if(req.body.SECRET_KEY == process.env.SECRET_KEY){
// const newAlert = req.body;
// delete newAlert['SECRET_KEY'];
// res.send('Alert sent!')
// return sendEventsToAll(newAlert);
// } else {
// res.send('ERROR!')
// }
// }
app.get('/', (req, res) => {res.send('Socket is up and running!')})
// app.get('/events', eventsHandler);
// app.post('/trigger', triggerEvent)
io.on('connection', (socket) => {
console.log(`Connection ${socket.id} established.`)
socket.on('sendRequestToServer', (data) => {
console.log(`Incoming request from ${data.name} with e-mail ${data.email}.`)
io.sockets.emit('sendRequestToAdmin', data)
})
socket.on('sendResponseToServer', (data) => {
if(data.action == 'reject'){
console.log(`Request ${data.socket_id} rejected by Admin.`)
} else {
console.log(`Request ${data.socket_id} accepted by Admin.`)
}
io.sockets.emit('sendResponseToClient', data)
})
socket.on('disconnect', (socket) => {
console.log('Disconnected.')
})
});
server.listen(3000, () => {
console.log('Socket.io server is running.')
})