-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (30 loc) · 1011 Bytes
/
index.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
$(function () {
const socket = io();
socket.on('user connection', function(){
$('#messages').append($('<li>').text('someone connected'));
});
socket.on('user disconnect', function(){
$('#messages').append($('<li>').text('someone disconnected'));
});
socket.on('user count', function(num){
$('#users').html(`users online ${num}`);
});
$('#set-name').on('click', function() {
let name = $('#alias').val();
$('#chat').html(name);
});
$('form').submit(function(e){
e.preventDefault();
let nickname = $('#chat').html();
let message = $('#m').val();
nickname === "" ? nickname = 'Anonymous User' :
socket.emit('chat message', { message: message, nickname: nickname });
$('#messages').append($('<li>').text(`${nickname}: ${message}`));
$('#m').val('');
return false;
});
socket.on('chat message', function(post){
const { message, nickname } = post;
$('#messages').append($('<li>').text(`${nickname}: ${message}`));
});
});