This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
116 lines (96 loc) · 3.46 KB
/
server.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
109
110
111
112
113
114
115
116
/**
* Entry / Start Point of the Websocket Applicartion
* + Setup
* + Create App
* + Run App
*
* @version 1.0
* @author Benjamin Thomas Schwertfeger
* @email [email protected]
* @github https://github.com/ProjectPepperHSB/WebsocketServer
* /
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> I M P O R T S <----- ----- ----- */
const
express = require('express'),
http = require('http'),
{
Server
} = require("socket.io");
const
app = express(),
server = http.createServer(app),
io = new Server(server);
const {
PORT,
URL
} = process.env.NODE_ENV == 'PROD' ? require('./config').PRODUCTION : require('./config').DEVELOPMENT;
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> S E T T I N G S <----- ----- ----- */
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(express.static('static'))
const undefined = 'undefined'
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> R O U T E S <----- ----- ----- */
app.get(`/`, (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
app.get(`/api/data`, (req, res) => {
try {
io.emit('chat message', req.query.message)
res.status(200).end();
} catch (e) {
res.json({
message: e,
status_code: 500
}).end();
}
})
app.post(`/api/data`, (req, res) => {
const body = req.body;
const data = body.data;
if (typeof data === undefined) res.status(400).end();
else {
if (JSON.stringify(data).toUpperCase().includes('UNKNOWN')) return res.status(200).end();
else {
try {
console.log(data)
io.emit('chat message', JSON.stringify(data))
res.status(200).end();
} catch (e) {
res.json({
message: e,
status_code: 500
}).end();
}
}
}
});
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> W E B S O C K E T - E V E N T - H A N D L E R <----- ----- ----- */
io.on('connection', (socket) => {
console.log('A user connected!');
socket.on('Disconnect', () => {
console.log('User disconnected');
});
socket.on('chat message', (msg) => {
io.emit('chat message', msg);
console.log(msg)
});
});
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> RUN APP <----- ----- ----- */
server.listen(PORT, () => {
console.log(`listening on ${URL}`);
});
/* * * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * * *
* * * ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- * * *
* * * -----> E O F <----- ----- ----- */