-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (71 loc) · 2.21 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
const http = require('http');
const url = require('url');
const dotenv = require('dotenv');
const express = require('express');
const WebSocket = require('ws');
const fetch = require('node-fetch');
const result = dotenv.config();
if (result.error) {
throw result.error;
}
const hostname = process.env.HOSTNAME || 'localhost';
const port = process.env.PORT || 3000;
const secretKey = process.env.SECRET_KEY;
const app = express();
app.use(express.json());
const httpServer = http.createServer(app);
const wsServer = new WebSocket.Server({ server: httpServer });
wsServer.on('connection', async (ws, request) => {
const query = url.parse(request.url, true).query;
console.log(query);
try {
const response = await fetch('/auth', {
method: 'POST',
body: JSON.stringify({
apiKey: query.apiKey,
token: query.token
}),
headers: { 'Content-Type': 'application/json' },
});
if (response.status === 200) {
const body = await response.json();
console.log(body);
ws.userId = body.userId;
ws.send("Hello!");
} else {
ws.send('Отказано в доступе!');
ws.close();
}
} catch (error) {
ws.send('Отказано в доступе!');
ws.close();
console.error(error);
}
});
app.post('/send', (request, response) => {
const body = request.body;
console.log(body);
if (body.secretKey === secretKey) {
response.status(200);
if (body.broadcast) {
wsServer.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(body.message);
}
});
} else {
for (client of wsServer.clients) {
if (client.userId == body.userId && client.readyState === WebSocket.OPEN) {
client.send(body.message);
break;
}
}
}
} else {
response.status(401);
}
response.end();
});
httpServer.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});