-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
151 lines (148 loc) Β· 4.98 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// All Required Modules & Packages ========================================
const path = require('path')
require(path.join(__dirname, '/database/connection.js'))
const express = require('express')
const app = express()
const expressLayouts = require('express-ejs-layouts')
const { ExpressPeerServer } = require('peer')
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const cookieParser = require('cookie-parser')
const peerServer = ExpressPeerServer(server, {
debug: true,
})
// Port Number & Listen ===================================================
const port = process.env.PORT || 80
server.listen(port)
// All Universal Middleware ===============================================
app.use(express.static(path.join(__dirname, 'static')))
app.set('view engine', 'ejs')
app.use(expressLayouts)
app.set('layout', 'layouts/base_format.ejs')
app.set('views', path.join(__dirname, 'views'))
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(cookieParser())
app.use('/peerJS', peerServer)
// Routes Middleware ======================================================
app.get('/', (req, res) => res.redirect('/home'))
app.use('/home', require('./routes/home'))
app.use('/room', require('./routes/room'))
app.use('/profile', require('./routes/profile'))
app.use('/features', require('./routes/features'))
app.use('/appearance', require('./routes/appearance'))
app.use('/developer', require('./routes/developer'))
app.use('/signup', require('./routes/signup'))
app.use('/login', require('./routes/login'))
app.use('/forget_password', require('./routes/forget_password'))
app.use('/logout', require('./routes/logout'))
app.use('/verify_otp', require('./routes/verify_otp'))
app.use('/*', require('./routes/notFound'))
// All Socket Events ======================================================
let allUsersGlobally = []
io.on('connection', socket => {
socket.on(
'new-user-joined-ingoing',
(roomID, newParticipantName, socketID, peerID) => {
let oneRoom = allUsersGlobally.find(
oneRoom => oneRoom.roomID === roomID
)
if (oneRoom === undefined) {
oneRoom = {
roomID,
hostId: socketID,
participantSocketID: [],
tempParticipantSocket: [],
}
allUsersGlobally.push(oneRoom)
oneRoom.participantSocketID.push(socketID)
socket.join(roomID)
} else {
oneRoom.tempParticipantSocket.push(socket)
setTimeout(() => {
socket
.to(oneRoom.hostId)
.emit(
'joining-permission-requested',
newParticipantName,
socketID,
peerID
)
}, 1000)
}
socket.on(
'join-permission-response',
(response, newParticipantName, socketID, peerID) => {
if (socket.id === oneRoom.hostId) {
const tempSocketObj =
oneRoom.tempParticipantSocket.shift()
if (response && tempSocketObj.connected) {
tempSocketObj.join(oneRoom.roomID)
oneRoom.participantSocketID.push(socketID)
io.in(roomID).emit(
'new-user-joined-outgoing',
newParticipantName,
socketID,
peerID
)
} else {
socket.to(socketID).emit('permission-denied')
}
}
}
)
socket.on(
'send-my-details-to-new-user',
(
oldParticipantName,
oldParticipantSocketID,
oldParticipantPeerID,
socketID
) => {
if (oneRoom.participantSocketID.includes(socket.id)) {
socket
.to(socketID)
.emit(
'receiving-old-participant-details',
oldParticipantName,
oldParticipantSocketID,
oldParticipantPeerID
)
}
}
)
socket.on('media-update-ingoing', (mediaType, peerID) => {
if (oneRoom.participantSocketID.includes(socket.id)) {
socket
.to(roomID)
.emit('media-update-outgoing', mediaType, peerID)
}
})
socket.on('sending-message', (chatMessage, time) => {
if (oneRoom.participantSocketID.includes(socket.id)) {
socket
.to(roomID)
.emit('receiving-message', chatMessage, time)
}
})
socket.on('disconnect', () => {
if (socket.id === oneRoom.hostId) {
allUsersGlobally.splice(
allUsersGlobally.indexOf(oneRoom),
1
)
socket.to(roomID).emit('host-left')
io.in(roomID).disconnectSockets()
} else if (oneRoom.participantSocketID.includes(socket.id)) {
oneRoom.participantSocketID.splice(
oneRoom.participantSocketID.indexOf(socket.id),
1
)
socket
.to(roomID)
.emit('participant-disconnected', socket.id)
}
})
}
)
})