@@ -25,7 +25,7 @@ $(function() {
25
25
26
26
var socket = io ( ) ;
27
27
28
- function addParticipantsMessage ( data ) {
28
+ const addParticipantsMessage = ( data ) => {
29
29
var message = '' ;
30
30
if ( data . numUsers === 1 ) {
31
31
message += "there's 1 participant" ;
@@ -36,7 +36,7 @@ $(function() {
36
36
}
37
37
38
38
// Sets the client's username
39
- function setUsername ( ) {
39
+ const setUsername = ( ) => {
40
40
username = cleanInput ( $usernameInput . val ( ) . trim ( ) ) ;
41
41
42
42
// If the username is valid
@@ -52,7 +52,7 @@ $(function() {
52
52
}
53
53
54
54
// Sends a chat message
55
- function sendMessage ( ) {
55
+ const sendMessage = ( ) => {
56
56
var message = $inputMessage . val ( ) ;
57
57
// Prevent markup from being injected into the message
58
58
message = cleanInput ( message ) ;
@@ -69,13 +69,13 @@ $(function() {
69
69
}
70
70
71
71
// Log a message
72
- function log ( message , options ) {
72
+ const log = ( message , options ) => {
73
73
var $el = $ ( '<li>' ) . addClass ( 'log' ) . text ( message ) ;
74
74
addMessageElement ( $el , options ) ;
75
75
}
76
76
77
77
// Adds the visual chat message to the message list
78
- function addChatMessage ( data , options ) {
78
+ const addChatMessage = ( data , options ) => {
79
79
// Don't fade the message in if there is an 'X was typing'
80
80
var $typingMessages = getTypingMessages ( data ) ;
81
81
options = options || { } ;
@@ -100,15 +100,15 @@ $(function() {
100
100
}
101
101
102
102
// Adds the visual chat typing message
103
- function addChatTyping ( data ) {
103
+ const addChatTyping = ( data ) => {
104
104
data . typing = true ;
105
105
data . message = 'is typing' ;
106
106
addChatMessage ( data ) ;
107
107
}
108
108
109
109
// Removes the visual chat typing message
110
- function removeChatTyping ( data ) {
111
- getTypingMessages ( data ) . fadeOut ( function ( ) {
110
+ const removeChatTyping = ( data ) => {
111
+ getTypingMessages ( data ) . fadeOut ( ( ) => {
112
112
$ ( this ) . remove ( ) ;
113
113
} ) ;
114
114
}
@@ -118,7 +118,7 @@ $(function() {
118
118
// options.fade - If the element should fade-in (default = true)
119
119
// options.prepend - If the element should prepend
120
120
// all other messages (default = false)
121
- function addMessageElement ( el , options ) {
121
+ const addMessageElement = ( el , options ) => {
122
122
var $el = $ ( el ) ;
123
123
124
124
// Setup default options
@@ -145,20 +145,20 @@ $(function() {
145
145
}
146
146
147
147
// Prevents input from having injected markup
148
- function cleanInput ( input ) {
148
+ const cleanInput = ( input ) => {
149
149
return $ ( '<div/>' ) . text ( input ) . html ( ) ;
150
150
}
151
151
152
152
// Updates the typing event
153
- function updateTyping ( ) {
153
+ const updateTyping = ( ) => {
154
154
if ( connected ) {
155
155
if ( ! typing ) {
156
156
typing = true ;
157
157
socket . emit ( 'typing' ) ;
158
158
}
159
159
lastTypingTime = ( new Date ( ) ) . getTime ( ) ;
160
160
161
- setTimeout ( function ( ) {
161
+ setTimeout ( ( ) => {
162
162
var typingTimer = ( new Date ( ) ) . getTime ( ) ;
163
163
var timeDiff = typingTimer - lastTypingTime ;
164
164
if ( timeDiff >= TYPING_TIMER_LENGTH && typing ) {
@@ -170,14 +170,14 @@ $(function() {
170
170
}
171
171
172
172
// 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 => {
175
175
return $ ( this ) . data ( 'username' ) === data . username ;
176
176
} ) ;
177
177
}
178
178
179
179
// Gets the color of a username through our hash function
180
- function getUsernameColor ( username ) {
180
+ const getUsernameColor = ( username ) => {
181
181
// Compute hash code
182
182
var hash = 7 ;
183
183
for ( var i = 0 ; i < username . length ; i ++ ) {
@@ -190,7 +190,7 @@ $(function() {
190
190
191
191
// Keyboard events
192
192
193
- $window . keydown ( function ( event ) {
193
+ $window . keydown ( event => {
194
194
// Auto-focus the current input when a key is typed
195
195
if ( ! ( event . ctrlKey || event . metaKey || event . altKey ) ) {
196
196
$currentInput . focus ( ) ;
@@ -207,26 +207,26 @@ $(function() {
207
207
}
208
208
} ) ;
209
209
210
- $inputMessage . on ( 'input' , function ( ) {
210
+ $inputMessage . on ( 'input' , ( ) => {
211
211
updateTyping ( ) ;
212
212
} ) ;
213
213
214
214
// Click events
215
215
216
216
// Focus input when clicking anywhere on login page
217
- $loginPage . click ( function ( ) {
217
+ $loginPage . click ( ( ) => {
218
218
$currentInput . focus ( ) ;
219
219
} ) ;
220
220
221
221
// Focus input when clicking on the message input's border
222
- $inputMessage . click ( function ( ) {
222
+ $inputMessage . click ( ( ) => {
223
223
$inputMessage . focus ( ) ;
224
224
} ) ;
225
225
226
226
// Socket events
227
227
228
228
// Whenever the server emits 'login', log the login message
229
- socket . on ( 'login' , function ( data ) {
229
+ socket . on ( 'login' , ( data ) => {
230
230
connected = true ;
231
231
// Display the welcome message
232
232
var message = "Welcome to Socket.IO Chat – " ;
@@ -237,45 +237,45 @@ $(function() {
237
237
} ) ;
238
238
239
239
// Whenever the server emits 'new message', update the chat body
240
- socket . on ( 'new message' , function ( data ) {
240
+ socket . on ( 'new message' , ( data ) => {
241
241
addChatMessage ( data ) ;
242
242
} ) ;
243
243
244
244
// 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 ) => {
246
246
log ( data . username + ' joined' ) ;
247
247
addParticipantsMessage ( data ) ;
248
248
} ) ;
249
249
250
250
// 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 ) => {
252
252
log ( data . username + ' left' ) ;
253
253
addParticipantsMessage ( data ) ;
254
254
removeChatTyping ( data ) ;
255
255
} ) ;
256
256
257
257
// Whenever the server emits 'typing', show the typing message
258
- socket . on ( 'typing' , function ( data ) {
258
+ socket . on ( 'typing' , ( data ) => {
259
259
addChatTyping ( data ) ;
260
260
} ) ;
261
261
262
262
// Whenever the server emits 'stop typing', kill the typing message
263
- socket . on ( 'stop typing' , function ( data ) {
263
+ socket . on ( 'stop typing' , ( data ) => {
264
264
removeChatTyping ( data ) ;
265
265
} ) ;
266
266
267
- socket . on ( 'disconnect' , function ( ) {
267
+ socket . on ( 'disconnect' , ( ) => {
268
268
log ( 'you have been disconnected' ) ;
269
269
} ) ;
270
270
271
- socket . on ( 'reconnect' , function ( ) {
271
+ socket . on ( 'reconnect' , ( ) => {
272
272
log ( 'you have been reconnected' ) ;
273
273
if ( username ) {
274
274
socket . emit ( 'add user' , username ) ;
275
275
}
276
276
} ) ;
277
277
278
- socket . on ( 'reconnect_error' , function ( ) {
278
+ socket . on ( 'reconnect_error' , ( ) => {
279
279
log ( 'attempt to reconnect has failed' ) ;
280
280
} ) ;
281
281
0 commit comments