-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (91 loc) · 3 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
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {"Content-Type": "text/plain"})
res.write('[ Affichage du contenu non-autorisé ]')
res.end();
});
var mysql = require('mysql')
var db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'tchat'
})
var users = [];
db.connect();
db.query('SELECT * FROM users ORDER BY id', function(err, data) {
users = data;
})
console.log(users);
var io = require('socket.io').listen(server);
var messages = [];
db.query('SELECT * FROM messages', function(err, data) {
messages = data;
})
var users_list = [];
var my_name = '';
io.sockets.on('connection', function(socket) {
socket.on('disconnect', function() {
if(my_name != '') {
users_list = users_list.filter(my_name => my_name > -1)
}
})
socket.on('VerifyLogin', function(login){
var is_ok_login = false;
users.forEach(function(user){
if(user.name === login.pseudo && user.password === login.password) {
var is_ok_connect = true;
users_list.forEach(function(el) {
if(el === login.pseudo) {
socket.emit('LoginIsValid', false);
socket.emit('ShowAlert', {
message: 'Cet utilisateur semble déjà connecté.',
type: 'error'
});
is_ok_connect = false;
is_ok_login = true;
}
})
if(is_ok_connect) {
socket.emit('LoginIsValid', true);
socket.emit('InitMessages', messages)
users_list.push(user.name);
io.sockets.emit('NewUser', users_list);
is_ok_login = true;
my_name = login.pseudo;
let moment = require('moment');
moment.locale('fr');
let time = moment(new Date).calendar();
io.sockets.emit('ShowMessages', {
text: login.pseudo + ' vient de se connecter au tchat.',
author: '',
date: time
});
}
}
})
if(!is_ok_login) {
socket.emit('LoginIsValid', false);
socket.emit('ShowAlert', {
message: 'Pseudo ou mot de passe invalide.',
type: 'error'
});
}
})
socket.on('NewMessage', function(data){
let moment = require('moment');
moment.locale('fr');
let time = moment(new Date).calendar();
let NewMessage = {
text: data.message,
author: data.name,
date: time
}
let sql = db.format('INSERT INTO messages (text, author, date) VALUES (?, ?, ?)', [data.message, data.name, time]);
db.query(sql, function(err, data) { })
io.sockets.emit('ShowMessages', NewMessage);
})
});
// db.end();
server.listen(8080);
console.log('>> Serveur lancé sur le port 8080...');