Skip to content

Commit 0279c47

Browse files
LetsDoughNutdarrachequesne
authored andcommitted
[docs] Convert the chat example to ES6 (socketio#3227)
1 parent 2917942 commit 0279c47

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

examples/chat/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var server = require('http').createServer(app);
66
var io = require('../..')(server);
77
var port = process.env.PORT || 3000;
88

9-
server.listen(port, function () {
9+
server.listen(port, () => {
1010
console.log('Server listening at port %d', port);
1111
});
1212

@@ -17,11 +17,11 @@ app.use(express.static(path.join(__dirname, 'public')));
1717

1818
var numUsers = 0;
1919

20-
io.on('connection', function (socket) {
20+
io.on('connection', (socket) => {
2121
var addedUser = false;
2222

2323
// when the client emits 'new message', this listens and executes
24-
socket.on('new message', function (data) {
24+
socket.on('new message', (data) => {
2525
// we tell the client to execute 'new message'
2626
socket.broadcast.emit('new message', {
2727
username: socket.username,
@@ -30,7 +30,7 @@ io.on('connection', function (socket) {
3030
});
3131

3232
// when the client emits 'add user', this listens and executes
33-
socket.on('add user', function (username) {
33+
socket.on('add user', (username) => {
3434
if (addedUser) return;
3535

3636
// we store the username in the socket session for this client
@@ -48,21 +48,21 @@ io.on('connection', function (socket) {
4848
});
4949

5050
// when the client emits 'typing', we broadcast it to others
51-
socket.on('typing', function () {
51+
socket.on('typing', () => {
5252
socket.broadcast.emit('typing', {
5353
username: socket.username
5454
});
5555
});
5656

5757
// when the client emits 'stop typing', we broadcast it to others
58-
socket.on('stop typing', function () {
58+
socket.on('stop typing', () => {
5959
socket.broadcast.emit('stop typing', {
6060
username: socket.username
6161
});
6262
});
6363

6464
// when the user disconnects.. perform this
65-
socket.on('disconnect', function () {
65+
socket.on('disconnect', () => {
6666
if (addedUser) {
6767
--numUsers;
6868

examples/chat/public/main.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $(function() {
2525

2626
var socket = io();
2727

28-
function addParticipantsMessage (data) {
28+
const addParticipantsMessage = (data) => {
2929
var message = '';
3030
if (data.numUsers === 1) {
3131
message += "there's 1 participant";
@@ -36,7 +36,7 @@ $(function() {
3636
}
3737

3838
// Sets the client's username
39-
function setUsername () {
39+
const setUsername = () => {
4040
username = cleanInput($usernameInput.val().trim());
4141

4242
// If the username is valid
@@ -52,7 +52,7 @@ $(function() {
5252
}
5353

5454
// Sends a chat message
55-
function sendMessage () {
55+
const sendMessage = () => {
5656
var message = $inputMessage.val();
5757
// Prevent markup from being injected into the message
5858
message = cleanInput(message);
@@ -69,13 +69,13 @@ $(function() {
6969
}
7070

7171
// Log a message
72-
function log (message, options) {
72+
const log = (message, options) => {
7373
var $el = $('<li>').addClass('log').text(message);
7474
addMessageElement($el, options);
7575
}
7676

7777
// Adds the visual chat message to the message list
78-
function addChatMessage (data, options) {
78+
const addChatMessage = (data, options) => {
7979
// Don't fade the message in if there is an 'X was typing'
8080
var $typingMessages = getTypingMessages(data);
8181
options = options || {};
@@ -100,15 +100,15 @@ $(function() {
100100
}
101101

102102
// Adds the visual chat typing message
103-
function addChatTyping (data) {
103+
const addChatTyping = (data) => {
104104
data.typing = true;
105105
data.message = 'is typing';
106106
addChatMessage(data);
107107
}
108108

109109
// Removes the visual chat typing message
110-
function removeChatTyping (data) {
111-
getTypingMessages(data).fadeOut(function () {
110+
const removeChatTyping = (data) => {
111+
getTypingMessages(data).fadeOut(() => {
112112
$(this).remove();
113113
});
114114
}
@@ -118,7 +118,7 @@ $(function() {
118118
// options.fade - If the element should fade-in (default = true)
119119
// options.prepend - If the element should prepend
120120
// all other messages (default = false)
121-
function addMessageElement (el, options) {
121+
const addMessageElement = (el, options) => {
122122
var $el = $(el);
123123

124124
// Setup default options
@@ -145,20 +145,20 @@ $(function() {
145145
}
146146

147147
// Prevents input from having injected markup
148-
function cleanInput (input) {
148+
const cleanInput = (input) => {
149149
return $('<div/>').text(input).html();
150150
}
151151

152152
// Updates the typing event
153-
function updateTyping () {
153+
const updateTyping = () => {
154154
if (connected) {
155155
if (!typing) {
156156
typing = true;
157157
socket.emit('typing');
158158
}
159159
lastTypingTime = (new Date()).getTime();
160160

161-
setTimeout(function () {
161+
setTimeout(() => {
162162
var typingTimer = (new Date()).getTime();
163163
var timeDiff = typingTimer - lastTypingTime;
164164
if (timeDiff >= TYPING_TIMER_LENGTH && typing) {
@@ -170,14 +170,14 @@ $(function() {
170170
}
171171

172172
// Gets the 'X is typing' messages of a user
173-
function getTypingMessages (data) {
174-
return $('.typing.message').filter(function (i) {
173+
const getTypingMessages = (data) => {
174+
return $('.typing.message').filter(i => {
175175
return $(this).data('username') === data.username;
176176
});
177177
}
178178

179179
// Gets the color of a username through our hash function
180-
function getUsernameColor (username) {
180+
const getUsernameColor = (username) => {
181181
// Compute hash code
182182
var hash = 7;
183183
for (var i = 0; i < username.length; i++) {
@@ -190,7 +190,7 @@ $(function() {
190190

191191
// Keyboard events
192192

193-
$window.keydown(function (event) {
193+
$window.keydown(event => {
194194
// Auto-focus the current input when a key is typed
195195
if (!(event.ctrlKey || event.metaKey || event.altKey)) {
196196
$currentInput.focus();
@@ -207,26 +207,26 @@ $(function() {
207207
}
208208
});
209209

210-
$inputMessage.on('input', function() {
210+
$inputMessage.on('input', () => {
211211
updateTyping();
212212
});
213213

214214
// Click events
215215

216216
// Focus input when clicking anywhere on login page
217-
$loginPage.click(function () {
217+
$loginPage.click(() => {
218218
$currentInput.focus();
219219
});
220220

221221
// Focus input when clicking on the message input's border
222-
$inputMessage.click(function () {
222+
$inputMessage.click(() => {
223223
$inputMessage.focus();
224224
});
225225

226226
// Socket events
227227

228228
// Whenever the server emits 'login', log the login message
229-
socket.on('login', function (data) {
229+
socket.on('login', (data) => {
230230
connected = true;
231231
// Display the welcome message
232232
var message = "Welcome to Socket.IO Chat – ";
@@ -237,45 +237,45 @@ $(function() {
237237
});
238238

239239
// Whenever the server emits 'new message', update the chat body
240-
socket.on('new message', function (data) {
240+
socket.on('new message', (data) => {
241241
addChatMessage(data);
242242
});
243243

244244
// Whenever the server emits 'user joined', log it in the chat body
245-
socket.on('user joined', function (data) {
245+
socket.on('user joined', (data) => {
246246
log(data.username + ' joined');
247247
addParticipantsMessage(data);
248248
});
249249

250250
// Whenever the server emits 'user left', log it in the chat body
251-
socket.on('user left', function (data) {
251+
socket.on('user left', (data) => {
252252
log(data.username + ' left');
253253
addParticipantsMessage(data);
254254
removeChatTyping(data);
255255
});
256256

257257
// Whenever the server emits 'typing', show the typing message
258-
socket.on('typing', function (data) {
258+
socket.on('typing', (data) => {
259259
addChatTyping(data);
260260
});
261261

262262
// Whenever the server emits 'stop typing', kill the typing message
263-
socket.on('stop typing', function (data) {
263+
socket.on('stop typing', (data) => {
264264
removeChatTyping(data);
265265
});
266266

267-
socket.on('disconnect', function () {
267+
socket.on('disconnect', () => {
268268
log('you have been disconnected');
269269
});
270270

271-
socket.on('reconnect', function () {
271+
socket.on('reconnect', () => {
272272
log('you have been reconnected');
273273
if (username) {
274274
socket.emit('add user', username);
275275
}
276276
});
277277

278-
socket.on('reconnect_error', function () {
278+
socket.on('reconnect_error', () => {
279279
log('attempt to reconnect has failed');
280280
});
281281

0 commit comments

Comments
 (0)