1 | 'use strict'; |
||
2 | |||
3 | var config = require('../config'); |
||
4 | var redis = require('redis').createClient; |
||
5 | var adapter = require('socket.io-redis'); |
||
6 | |||
7 | var Room = require('../models/room'); |
||
8 | |||
9 | /** |
||
10 | * Encapsulates all code for emitting and listening to socket events |
||
11 | * |
||
12 | */ |
||
13 | var ioEvents = function(io) { |
||
14 | |||
15 | // Rooms namespace |
||
16 | io.of('/rooms').on('connection', function(socket) { |
||
17 | |||
18 | // Create a new room |
||
19 | socket.on('createRoom', function(title) { |
||
20 | Room.findOne({'title': new RegExp('^' + title + '$', 'i')}, function(err, room){ |
||
21 | if(err) throw err; |
||
0 ignored issues
–
show
|
|||
22 | if(room){ |
||
23 | socket.emit('updateRoomsList', { error: 'Room title already exists.' }); |
||
24 | } else { |
||
25 | Room.create({ |
||
26 | title: title |
||
27 | }, function(err, newRoom){ |
||
28 | if(err) throw err; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
29 | socket.emit('updateRoomsList', newRoom); |
||
30 | socket.broadcast.emit('updateRoomsList', newRoom); |
||
31 | }); |
||
32 | } |
||
33 | }); |
||
34 | }); |
||
35 | }); |
||
36 | |||
37 | // Chatroom namespace |
||
38 | io.of('/chatroom').on('connection', function(socket) { |
||
39 | |||
40 | // Join a chatroom |
||
41 | socket.on('join', function(roomId) { |
||
42 | Room.findById(roomId, function(err, room){ |
||
43 | if(err) throw err; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
44 | if(!room){ |
||
45 | // Assuming that you already checked in router that chatroom exists |
||
46 | // Then, if a room doesn't exist here, return an error to inform the client-side. |
||
47 | socket.emit('updateUsersList', { error: 'Room doesnt exist.' }); |
||
48 | } else { |
||
49 | // Check if user exists in the session |
||
50 | if(socket.request.session.passport == null){ |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | Room.addUser(room, socket, function(err, newRoom){ |
||
55 | |||
56 | // Join the room channel |
||
57 | socket.join(newRoom.id); |
||
58 | |||
59 | Room.getUsers(newRoom, socket, function(err, users, cuntUserInRoom){ |
||
60 | if(err) throw err; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
61 | |||
62 | // Return list of all user connected to the room to the current user |
||
63 | socket.emit('updateUsersList', users, true); |
||
64 | |||
65 | // Return the current user to other connecting sockets in the room |
||
66 | // ONLY if the user wasn't connected already to the current room |
||
67 | if(cuntUserInRoom === 1){ |
||
68 | socket.broadcast.to(newRoom.id).emit('updateUsersList', users[users.length - 1]); |
||
69 | } |
||
70 | }); |
||
71 | }); |
||
72 | } |
||
73 | }); |
||
74 | }); |
||
75 | |||
76 | // When a socket exits |
||
77 | socket.on('disconnect', function() { |
||
78 | |||
79 | // Check if user exists in the session |
||
80 | if(socket.request.session.passport == null){ |
||
81 | return; |
||
82 | } |
||
83 | |||
84 | // Find the room to which the socket is connected to, |
||
85 | // and remove the current user + socket from this room |
||
86 | Room.removeUser(socket, function(err, room, userId, cuntUserInRoom){ |
||
87 | if(err) throw err; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
88 | |||
89 | // Leave the room channel |
||
90 | socket.leave(room.id); |
||
91 | |||
92 | // Return the user id ONLY if the user was connected to the current room using one socket |
||
93 | // The user id will be then used to remove the user from users list on chatroom page |
||
94 | if(cuntUserInRoom === 1){ |
||
95 | socket.broadcast.to(room.id).emit('removeUser', userId); |
||
96 | } |
||
97 | }); |
||
98 | }); |
||
99 | |||
100 | // When a new message arrives |
||
101 | socket.on('newMessage', function(roomId, message) { |
||
102 | |||
103 | // No need to emit 'addMessage' to the current socket |
||
104 | // As the new message will be added manually in 'main.js' file |
||
105 | // socket.emit('addMessage', message); |
||
106 | |||
107 | socket.broadcast.to(roomId).emit('addMessage', message); |
||
108 | }); |
||
109 | |||
110 | }); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Initialize Socket.io |
||
115 | * Uses Redis as Adapter for Socket.io |
||
116 | * |
||
117 | */ |
||
118 | var init = function(app){ |
||
119 | |||
120 | var server = require('http').Server(app); |
||
121 | var io = require('socket.io')(server); |
||
122 | |||
123 | // Force Socket.io to ONLY use "websockets"; No Long Polling. |
||
124 | io.set('transports', ['websocket']); |
||
125 | |||
126 | // Using Redis |
||
127 | let port = config.redis.port; |
||
128 | let host = config.redis.host; |
||
129 | let password = config.redis.password; |
||
130 | let pubClient = redis(port, host, { auth_pass: password }); |
||
131 | let subClient = redis(port, host, { auth_pass: password, return_buffers: true, }); |
||
132 | io.adapter(adapter({ pubClient, subClient })); |
||
133 | |||
134 | // Allow sockets to access session data |
||
135 | io.use((socket, next) => { |
||
136 | require('../session')(socket.request, {}, next); |
||
137 | }); |
||
138 | |||
139 | // Define all Events |
||
140 | ioEvents(io); |
||
141 | |||
142 | // The server object will be then used to list to a port number |
||
143 | return server; |
||
144 | } |
||
145 | |||
146 | module.exports = init; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.