-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (112 loc) · 3.2 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
const express = require('express')
const http = require('http')
const { Server: SocketIO } = require('socket.io')
const shortid = require('shortid')
const app = express()
const server = http.Server(app)
const io = new SocketIO(server)
const port = process.env.PORT || 3000
const Room = function (user) {
this.id = shortid.generate()
this.users = new Set([user])
return this
}
Room.prototype.addUser = function (newUser) {
this.users.add(newUser)
return this
}
Room.prototype.removeUser = function (user) {
this.users.delete(user)
return this
}
Room.prototype.getUserCount = function () {
return this.users.size
}
Room.prototype.getId = function () {
return this.id
}
Room.prototype.toString = function () {
return `${this.id} - ${this.users.size}`
}
const RoomList = function () {
this.rooms = []
}
RoomList.prototype.addRoom = function (user) {
const newRoom = new Room(user)
this.rooms.push(newRoom)
return newRoom.id
}
RoomList.prototype.removeRoom = function (roomId) {
this.rooms.splice(this.rooms.findIndex(room => room.id === roomId), 1)
}
RoomList.prototype.removeUserFromRoom = function (user, roomId) {
if (
this
.findById(roomId)
.removeUser(user)
.getUserCount() === 0
) {
this.removeRoom(roomId)
}
return this
}
RoomList.prototype.roomExist = function (roomId) {
return Boolean(this.findById(roomId))
}
RoomList.prototype.addUserToRoom = function (user, roomId) {
const room = this.findById(roomId)
if (room.getUserCount() === 1) {
room.addUser(user)
return true
}
return false
}
RoomList.prototype.findById = function (roomId) {
return this.rooms.find(room => room.getId() === roomId)
}
RoomList.prototype.toString = function () {
return this.rooms.join(', ')
}
const roomList = new RoomList()
app.use(express.static(`${__dirname}/client/public`))
io.on('connection', socket => {
let roomId = socket.handshake.query.roomId
console.log(`User Connected, roomId: ${roomId}`)
if (roomId && roomList.roomExist(roomId)) {
const added = roomList.addUserToRoom(socket.id, roomId)
if (added) {
socket.join(roomId)
socket.broadcast.to(roomId).emit('join')
console.log(`User enter the room. ${roomList}`)
} else {
socket.emit(
'tryAnotherRoom',
'No one needs you here... This is a cruel world, what did you expect?'
)
roomId = roomList.addRoom(socket.id)
socket.join(roomId)
console.log(`New room added, user joined, roomList: ${roomList}`)
socket.emit('room', roomId)
}
} else {
if (roomId) {
socket.emit(
'tryAnotherRoom',
'That room does not exist'
)
}
roomId = roomList.addRoom(socket.id)
socket.join(roomId)
console.log(`New room added, user joined, roomList: ${roomList}`)
socket.emit('room', roomId)
}
socket.on('message', msg => socket.broadcast.to(roomId).emit('message', msg))
socket.on('disconnect', () => {
roomList.removeUserFromRoom(socket.id, roomId)
socket.broadcast.to(roomId).emit('tryAnotherRoom', 'Your friend is left the building...')
console.log(`User Disconnected, roomList: ${roomList}`)
})
})
server.listen(port, () => {
console.log(`[INFO] Server is running on: http://localhost:${port}`)
})