-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
63 lines (51 loc) · 1.53 KB
/
Copy pathjavascript.js
File metadata and controls
63 lines (51 loc) · 1.53 KB
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
61
62
63
// /public/javascript.js
// Get the current username from the cookies
var user = cookie.get('user');
if (!user) {
// Ask for the username if there is none set already
user = prompt('Choose a username:');
if (!user) {
alert('We cannot work with you like that!');
} else {
// Store it in the cookies for future use
cookie.set('user', user);
}
}
// ./public/javascript.js
// Get the current username from the cookies
var user = cookie.get('user');
if (!user) {
// Ask for the username if there is none set already
user = prompt('Choose a username:');
if (!user) {
alert('We cannot work with you like that!');
} else {
// Store it in the cookies for future use
cookie.set('user', user);
}
}
var socket = io();
// The user count. Can change when someone joins/leaves
socket.on('count', function (data) {
$('.user-count').html(data);
});
// When we receive a message
// it will be like { user: 'username', message: 'text' }
socket.on('message', function (data) {
$('.chat').append('<p><strong>' + data.user + '</strong>: ' + data.message + '</p>');
});
// When the form is submitted
$('form').submit(function (e) {
// Avoid submitting it through HTTP
e.preventDefault();
// Retrieve the message from the user
var message = $(e.target).find('input').val();
// Send the message to the server
socket.emit('message', {
user: cookie.get('user') || 'Anonymous',
message: message
});
// Clear the input and focus it for a new message
e.target.reset();
$(e.target).find('input').focus();
});