-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (115 loc) · 3.13 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require('express');
const cors = require('cors');
const { v4 } = require('uuid');
const app = express();
const SocketIO = require('socket.io');
const https = require('https');
const fs = require('fs');
const httpsServer = https
.createServer(
{
cert: fs.readFileSync(
'/etc/letsencrypt/live/vps49384.publiccloud.com.br/fullchain.pem',
),
key: fs.readFileSync(
'/etc/letsencrypt/live/vps49384.publiccloud.com.br/privkey.pem',
),
},
app,
)
.listen(7000);
const io = SocketIO(httpsServer, {
cors: {
origin: '*',
methods: ['GET', 'POST'],
},
});
let messages = [];
let nicknames = [];
//Routes
app.get('/', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
app.use(cors());
res.send(v4());
});
//Io Events
io.on('connect', (socket) => {
//Join treatment
const room = socket.handshake.query.room;
const user = socket.id;
let myNickname = false;
socket.join(room);
io.in(room).emit('getNickname', nicknames[room]);
socket.on('newUserConnected', () => {
socket.emit('getUser', user);
let userList = io.sockets.adapter.rooms.get(room);
let arrUserList = undefined;
if (userList !== undefined) {
arrUserList = Array.from(userList);
} else {
arrUserList = arrUserList;
}
io.in(room).emit('getUserList', Array.from(userList));
if (messages[room]) {
io.in(room).emit('getChat', messages[room]);
}
});
//disconect treatment
socket.on('disconnect', () => {
let userList = io.sockets.adapter.rooms.get(room);
let arrUserList = undefined;
if (userList !== undefined) {
arrUserList = Array.from(userList);
} else {
arrUserList = arrUserList;
}
if (myNickname) {
delete nicknames[room][myNickname];
}
io.in(room).emit('getUserList', arrUserList);
io.in(room).emit('removeVideo', user);
});
//Messages treatment
socket.on('newMessage', (args) => {
if (messages[room] === undefined) {
messages[room] = [];
messages[room].push({ message: args.message, user: args.user });
} else {
messages[room].push({ message: args.message, user: args.user });
}
io.in(room).emit('getChat', messages[room]);
});
//Nickname change
socket.on('sendNickname', (arg) => {
if (nicknames[room] === undefined) {
nicknames[room] = [];
nicknames[room].push(arg);
myNickname = 0;
} else {
let index = false;
for (let i = -1; i <= nicknames[room].length; ) {
if (nicknames[room][i] !== undefined) {
if (nicknames[room][i].user === user) {
index = i;
}
}
i++;
}
if (index !== false) {
nicknames[room][index] = arg;
myNickname = index;
} else {
nicknames[room].push(arg);
myNickname = nicknames[room].length;
}
}
io.in(room).emit('getNickname', nicknames[room]);
});
//Empty room treatment
io.of('/').adapter.on('delete-room', (room) => {
if (messages[room] !== undefined) {
delete messages[room];
}
});
});