/
server.ts
110 lines (95 loc) · 3.55 KB
/
server.ts
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
import compression from 'compression';
import express from 'express';
import fs from 'fs';
import { Server } from 'http';
import https from 'https';
import path from 'path';
import socketio from 'socket.io';
const app = express();
const http = new Server(app);
const httpsServer = https.createServer({
key: fs.readFileSync('/etc/letsencrypt/live/sacalerts.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/sacalerts.com/fullchain.pem'),
}, app);
const io = socketio(httpsServer);
const port = process.env.PORT || 3001;
const getRoomSockets = (roomName: string) => {
const participants = [];
if (io.sockets.adapter.rooms[roomName]) {
Object.keys(io.sockets.adapter.rooms[roomName].sockets).forEach(socketId => {
participants.push(io.sockets.connected[socketId])
});
}
return participants;
}
app.use(compression());
app.use(express.static(path.join(__dirname, 'dist/client')))
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'dist/client/index.html'));
});
io.on('connection', function (socket) {
socket.on('enter', async function (roomName, username) {
username = username.substring(0, 20);
console.log(`${new Date()}: ${username} entering ${roomName} with ${socket.id} socket`);
(socket as any).username = username;
(socket as any).roomName = roomName;
// name update
if (getRoomSockets(roomName).find(s => s.id === socket.id)) {
console.log(`Updating name to ${username}`)
const participants = getRoomSockets(roomName).map(socket => socket.username)
getRoomSockets(roomName).forEach(socket => {
io.to(socket.id).emit('roomUpdated', participants, socket.username);
})
return;
}
const room = io.sockets.adapter.rooms[roomName];
if (room && room.length === 2) {
console.log(`${roomName} is full`)
return;
}
try {
await new Promise((res, rej) => {
socket.join(roomName, (err) => {
if (err) { rej(err) }
res()
});
});
} catch (e) {
console.log(`Failed to join ${roomName} socket channel`)
return;
}
console.log(`Added socket to ${roomName} message channel`);
const participants = getRoomSockets(roomName).map(socket => socket.username)
console.log('emitting roomUpdated', participants)
getRoomSockets(roomName).forEach(socket => {
io.to(socket.id).emit('roomUpdated', participants, socket.username);
})
if (room && room.length === 2) {
io.to(socket.id).emit('initialize');
}
});
socket.on('disconnect', function () {
const { username, roomName } = socket as any;
console.log(`${new Date()}: ${username} leaving ${roomName}`);
const participants = getRoomSockets(roomName).map(socket => socket.username)
console.log('emitting roomUpdated', participants)
getRoomSockets(roomName).forEach(socket => {
io.to(socket.id).emit('roomUpdated', participants, socket.username);
})
});
socket.on('signal', function (msg) {
const { username, roomName } = socket as any;
console.log(`${new Date()}: ${username}:${socket.id} signaled in ${roomName}`)
const participants = getRoomSockets(roomName).filter(roomSocket => roomSocket.id !== socket.id)
if (participants.length === 1) {
console.log(`Routing signal to ${participants[0].username}:${participants[0].id} in ${roomName}`);
io.to(participants[0].id).emit('signal', msg);
}
});
});
http.listen(port, function () {
console.log('listening on *:' + port);
});
httpsServer.listen(3000, function () {
console.log('https listening on *:' + 3000);
});