-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwss.js
63 lines (60 loc) · 1.93 KB
/
wss.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
const querystring = require('querystring');
const url = require('url');
const WebSocket = require('ws');
const models = require('./models');
const photoServer = new WebSocket.Server({ noServer: true });
photoServer.on('connection', async (ws, req) => {
const userId = req.user.id;
const photoId = req.photo.id;
// eslint-disable-next-line no-param-reassign
ws.info = { userId, photoId };
ws.on('message', async (data) => {
// broadcast back to other users viewing the same photo
const payload = data.toString();
photoServer.clients.forEach((client) => {
if (client.info.photoId === photoId && client.info.userId !== userId) {
client.send(payload);
}
});
});
ws.send(JSON.stringify({ timestamp: new Date().getTime() }));
});
function configure(server, app) {
server.on('upgrade', (req, socket, head) => {
app.sessionParser(req, {}, async () => {
const query = querystring.parse(url.parse(req.url).query);
// ensure user logged in
if (req.session?.passport?.user) {
req.user = await models.User.findByPk(req.session.passport.user);
}
if (!req.user) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
// connect based on pathname
const { pathname } = url.parse(req.url);
switch (pathname) {
case '/photo':
if (query.id && query.id !== 'undefined') {
req.photo = await models.Photo.findByPk(query.id);
}
if (!req.photo) {
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
return;
}
photoServer.handleUpgrade(req, socket, head, (ws) => {
photoServer.emit('connection', ws, req);
});
break;
default:
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
}
});
});
}
module.exports = {
configure,
};