forked from makers-upv/chat
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.js
More file actions
29 lines (27 loc) · 795 Bytes
/
chat.js
File metadata and controls
29 lines (27 loc) · 795 Bytes
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
// Guardar el nombre del usuario en el back-end y comunicarlo a todos
exports.login = ctx => {
ctx.socket.user = ctx.data;
console.log('Login:', ctx.socket.user);
return ctx.io.emit('login', {
user: ctx.socket.user,
time: new Date()
});
};
// Cuando alguien envía un mensaje, reenviarlo a todo el mundo
exports.message = ctx => {
console.log('Message:', ctx.data);
ctx.io.emit('message', {
user: ctx.socket.user,
text: ctx.data,
time: new Date()
});
};
// Cuando alguien hace logout mostrarselo también a todo el mundo
exports.logout = ctx => {
console.log('Logout:', ctx.socket.user);
if (!ctx.socket.user) return; // Para los que entran y salen sin hacer login
return ctx.io.emit('logout', {
user: ctx.socket.user,
time: new Date()
});
};