-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
100 lines (90 loc) · 3.57 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
<!doctype html>
<html>
<head>
<title>Chat app using socket.io</title>
<link rel="stylesheet" href="/css/styles.css"></link>
</head>
<body>
<div>
<div id="name-box" class="chat" style="height: 20vh;">
<div class="chat-title">
<h1>Chat Box</h1>
<h2>Welcome to Chat Box</h2>
<figure class="avatar">
<img src="/images/chat.png" /></figure>
</div>
<div class="messages-content" style="color: white; text-align: center; font-family: open-sans">
<h1>Enter Username</h1>
</div>
<div class="message-box">
<input type="text" id="username" class="message-input" placeholder="username">
<button id="usernameBtn" class="message-submit">Enter</button>
</div>
</div>
<div id="chat-box" class="chat" style="display: none">
<div class="chat-title">
<h1 id="userBox" ></h1>
<h2 style="color: #00c79c">Online</h2>
<figure class="avatar">
<img id="userPic" src="" /></figure>
</div>
<div class="messages">
<div id="messages" class="messages-content" style="padding: 5px;"></div>
</div>
<div class="message-box">
<form action="">
<input type="text" id="m" class="message-input" placeholder="Type message...">
<button type="submit" id="sendMessageBtn" class="message-submit">Send</button>
</form>
</div>
</div>
<div class="bg"></div>
</div>
<!-- Injecting the socket.io script -->
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var nameBox = $("#name-box");
var chat = $('#chat-box').hide();
var username = $("#username");
var socket = io();
$('#usernameBtn').click(function() {
nameBox.hide();
chat.show();
console.log(username.val());
$("#userBox").text(username.val());
var image = "https://api.adorable.io/avatars/48/" + username.val() + ".png";
console.log(image);
$("#userPic").attr('src',image)
})
//Trigger send button event
$('#sendMessageBtn').click(function(){
//Emit message to socket.io
var messageText = $('#m').val();
var messageEnvelop = {
'username' : username.val(),
'message' : messageText
};
socket.emit('chat message', messageEnvelop);
$('#messages').append($("<div class='message message-personal new'>" + messageText + "</div>"))
//Reset the chat form
$('#m').val('');
updateScroll();
return false;
});
function updateScroll(){
var element = document.getElementById("messages");
element.scrollTop = element.scrollHeight;
}
//Receive Messages
socket.on('chat message', function(msg){
//Append message to the message list
$('#messages').append($("<div class='message new'><span class='username'>" + msg.username + "</span></br><figure class='avatar'><img src='https://api.adorable.io/avatars/48/" + msg.username + ".png'/></figure>"
+ msg.message + "</div>"));
updateScroll();
});
});
</script>
</body>
</html>