-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserveur.js
140 lines (113 loc) · 3.03 KB
/
serveur.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
const express = require('express')
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
const formatMessage = require('./utils/messages');
const {
userJoin,
getCurrentUser,
userLeave,
getRoomUsers,
getUsers,
findByName,
updateRoom
} = require('./utils/users');
const port = 3000
app.use(express.static(__dirname + '/public'));
users = []
rooms = [
{
value: "Discussion 1",
name: "Discussion 1"
},
{
value: "Discussion 2",
name: "Discussion 2"
},
{
value: "Discussion 3",
name: "Discussion 3"
},
{
value: "creer",
name: "Crée sa discussion"
},
]
io.on('connection', socket => {
socket.on('joinRoom', ({ username, room }) => {
newusers = false
const user = userJoin(socket.id, username, room);
socket.join(user.room);
socket.emit('message', formatMessage("Serveur", 'Bienvenue dans la conversation!'));
if (room == "individuel" ) {
io.emit('roomUsers', {
room: user.room,
users: getUsers()
});
} else {
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
socket.on('chatMessage', msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
socket.on('individuelRoom', (roomind) => {
const user1 = findByName(roomind.username1)
user1.room = roomind.room
user1.id = socket.id
updateRoom(user1)
socket.on('joinRoom').join(user1.room);
io.emit('individuelRoom', {
demandeur: roomind.username1,
reception: roomind.username2,
roomindivi: roomind.room
})
console.log(getUsers())
});
socket.on('acceptioninvi', (data)=> {
const user1 = findByName(data.username)
user1.room = data.room
user1.id = socket.id
updateRoom(user1)
socket.join(user1.room);
})
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit(
'message',
formatMessage("Serveur", `${user.username} vient de se deconnecter`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
socket.on('login', user => {
user.id = socket.client.id
users.push(user)
console.log('users : ',users)
if (users) {
socket.emit('login', {
server: 'Client connecté',
users,
user
})
}
});
socket.emit("sendRooms", rooms);
socket.on('addroom', addRoom => {
console.log("addroom", addRoom)
rooms.push(addRoom)
console.log(rooms)
});
});
server.listen(port, ()=>{
console.log("Le serveur fonctionne désormais sur le port:", port,)
});