-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
122 lines (111 loc) · 3.78 KB
/
index.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div id="username">
<h1>Welcome to FSE Chatroom</h1>
<div class="progress">
<div class="progress-bar" style="width: 100%">
</div>
</div>
<form id="usernameForm" action="">
<input class="form-control" type="text" id="usernameInput" autocomplete="off" placeholder="Enter your name"/>
<button class="btn btn-primary btn-lg">Enter</button>
<div id="error" class="text-danger"></div>
</form>
</div>
<div id="message" style="display: none;">
<div class="header">
<div id="headerTitle">FSE Chatroom</div>
<div class="logout-button">
<a id="logoutButton">Logout</a>
</div>
</div>
<div id="headerSeparator"></div>
<div id="scrollingMessages">
<ul id="messages"></ul>
</div>
<form id="messageForm" action="">
<input class="form-control input-lg" type="text" id="messageInput" autocomplete="off" />
<div id="sendbutton"><button class="btn btn-primary btn-lg">Send</button></div>
</form>
</div>
<script>
var socket = io.connect("http://localhost:3000");
$('#usernameInput').keypress(function() {
$('#error').text('');
$(this).focus();
});
$('#usernameForm').submit(function(){
var user = $('#usernameInput').val();
console.log('user: ' + user);
if (user.trim().length > 0) {
console.log('emitting user entered');
socket.emit('user entered', user);
} else {
$('#error').text('You must enter a valid username');
}
$('#usernameInput').val('');
return false;
});
socket.on('user validated', function(username, rows){
$('#username').hide('slow');
$('#message').show('slow');
rows.forEach(function(row){
$('#messages').append($('<li>').html('<div class="msg-container"><div class="username">' +row.username + '</div><div class="timestamp">' + getPresentableTime(row.timestamp) + '</div></div><div class="msg"> ' +row.message + '</div>'));
});
});
socket.on('username error', function(error){
$('#error').text(error);
});
$('#messageForm').submit(function() {
var message = $('#messageInput').val();
if (message.trim().length > 0) {
socket.emit('chat message', {
name : '',
msg : message,
timestamp : Date.now()
});
}
$('#messageInput').val('');
return false;
});
$('#logoutButton').click(function(){
socket.emit('logout',{});
});
socket.on('chat message', function(data){
if($('#message').is(':visible')) {
$('#messages').append($('<li>').html('<div class="msg-container"><div class="username">' +data.name + '</div><div class="timestamp">' + getPresentableTime(data.timestamp) + '</div></div><div class="msg"> ' +data.msg + '</div>'));
}
});
socket.on('logged out', function() {
$('#username').show('slow');
$('#message').hide('slow');
$('#messages').empty();
});
socket.on('user login', function(message) {
if ($('#message').is(':visible')) {
$('#messages').append($('<li>').html('<div class="broadcast">'+ message + ' just joined</div>'));
}
});
socket.on('user exit', function(message) {
if ($('#message').is(':visible')) {
$('#messages').append($('<li>').html('<div class="broadcast">'+ message + ' has left the room</div>'));
}
});
function getPresentableTime(timestamp) {
var date = new Date(Number(timestamp));
var dateString = date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
return dateString;
}
</script>
</body>
</html>