-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
139 lines (111 loc) · 3.83 KB
/
app.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
// MODULES
let express = require('express')
let http = require('http')
let socketio = require('socket.io')
let morgan = require('morgan')
let striptags = require('striptags')
let config = require('./config')
// CONSTANTES
const app = express()
const server = http.Server(app)
const io = socketio(server)
const port = config.express.port
const options = {
root: __dirname+'/views'
}
// VARIABLES GLOBALES
let usernames = []
// MIDDLEWARES
app.use(express.static(options.root))
app.use(morgan('dev'))
// ROUTES
app.get('/', (req, res) => {
res.redirect('/home')
})
app.get('/home', (req, res) => {
res.sendFile('index.html', options)
})
app.get('/params/:name', (req, res) => {
res.send(req.params.name)
})
// IO
io.on('connection', function (socket) {
console.log('a user connected : ' + socket.id);
// Traitement pour l'assignation d'un username
socket.on('setUsername', (usernameWanted) => {
// Traitement de la chaine de caractères
usernameWanted = striptags(usernameWanted.trim())
// Vérification de l'unicité de l'username
let usernameTaken = false
for (let socketid in usernames) {
if (usernames[socketid] == usernameWanted)
usernameTaken = true
}
let timeFakeLoading = 0
setTimeout(() => {
// Traitement final
if (usernameTaken) {
socket.emit('rejectUsername', usernameWanted)
} else {
socket.join('users', () => {
usernames[socket.id] = usernameWanted
let justUsernames = getUsernames()
socket.emit('acceptUsername', usernameWanted, justUsernames, getSocketIDs())
socket.to('users').emit('newUser', usernameWanted, socket.id, justUsernames)
})
}
}, timeFakeLoading);
})
// Réception d'un message
socket.on('sendMessage', (text, dataChat) => {
text = striptags(text.trim())
if (text != '') {
let data = getVariablesDataChat(dataChat, socket.id)
socket.to(data.roomToSend).emit('newMessage', text, usernames[socket.id], data.chatToShow)
socket.emit('confirmMessage', text, data.dataChat)
}
})
// Informations sur l'écriture d'un message
socket.on('startWriting', (dataChat) => {
let data = getVariablesDataChat(dataChat, socket.id)
socket.to(data.roomToSend).emit('userStartWriting', usernames[socket.id], data.chatToShow)
})
socket.on('stopWriting', (dataChat) => {
let data = getVariablesDataChat(dataChat, socket.id)
socket.to(data.roomToSend).emit('userStopWriting', data.chatToShow)
})
// Déconnexion de l'utilisateur
socket.on('disconnect', () => {
if (usernames[socket.id]) {
delete usernames[socket.id]
socket.to('users').emit('leftUser', socket.id, getUsernames())
}
})
});
// Lancement de l'application
server.listen(port, () => console.log('Server started on port ' + port))
// Renvoie un array contenant uniquement les usernames sans index
function getUsernames() {
let users = []
for (let socketid in usernames) {
users.push(usernames[socketid])
}
return users
}
// Renvoie un array contenant uniquement les socketID sans index
function getSocketIDs() {
let socketIDs = []
for (let socketid in usernames) {
socketIDs.push(socketid)
}
return socketIDs
}
// Envoie toutes les variables découlant de DataChat
function getVariablesDataChat(dataChat, socketID) {
dataChat = dataChat == null ? 'person0' : dataChat
return {
roomToSend: dataChat == 'person0' ? 'users' : dataChat,
chatToShow: dataChat == 'person0' ? dataChat : socketID,
dataChat: dataChat
}
}