-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
60 lines (51 loc) · 2.06 KB
/
socket.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
const SocketIo = require('socket.io');
const cookieSession = require('./middlewares/cookie-session');
const ROOMS = ['general', 'developers', 'news', 'random'];
const DEFAULT_ROOM = ROOMS[0];
module.exports = function(server) {
const io = SocketIo(server);
// Share session between express and socket.io to retrieve the username
// from the session cookie
io.use((socket, next) => {
cookieSession(socket.request, socket.request.res, next);
});
io.on('connection', (socket) => {
const user = socket.request.session.user;
console.log('connection', 'user:' + user)
// Join to the default room
socket.join(DEFAULT_ROOM);
// Send available rooms to the clien
socket.emit('rooms', ROOMS);
// Send a message to the rest of the room
socket.broadcast.emit('info', `"${user}" has joined to "${DEFAULT_ROOM}"`);
// Listen all messages sended by the clients
socket.on('send-message', (message, date, room) => {
console.log('send-message', message, date, room);
// Send the message to the rest
socket.to(room).broadcast.emit('receive-message', { user, message, date });
// Send the message to the client
socket.emit('sent-message', { user, message, date });
});
// Listen all messages sended by the clients
socket.on('change-room', (from, to) => {
console.log('change-room', from, to);
// Notify to the room that someone has exited
socket.to(from).broadcast.emit('info', `"${user}" has left the room`);
// Leave the old room
socket.leave(from);
// Join the new room
socket.join(to);
// Send the join message to the new room
socket.to(to).broadcast.emit('info', `"${user}" has joined`);
// Send a notification to the client
socket.emit('info', `you joined "${to}"`);
});
// Listen disconnections
socket.on('disconnect', () =>{
console.log('disconnect', 'user:' + user);
// Send a message to the room when someone disconnects
socket.broadcast.emit('alert', `"${user}" has disconnected`);
});
});
return io;
};