-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
96 lines (95 loc) · 3.38 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
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<!-- <script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
.mainwrap {display: none}
.mainwrap form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
.mainwrap form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
.mainwrap form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; padding-bottom: 100px }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#connections { min-height: 30px; background-color: #ff5252; color: whitesmoke; display: block; }
</style>
</head>
<body>
<div class="nameform">
<form action="" id="nform">
<input type="text" autocomplete="off" id="n" autofocus placeholder="Enter Your Name"/>
<button>Send</button>
</form>
</div>
<div class="mainwrap">
<div id="connections"></div>
<div id="activeUsers"></div>
<ul id="messages">
<div class="pmessages"></div>
</ul>
<form action="" id="mform">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script>
$(function () {
var xhr = new XMLHttpRequest();
xhr.open('GET','/prev-messages',true);
xhr.onload = function(){
if(this.status == 200){
var messages = JSON.parse(this.responseText);
messages.forEach(function(element) {
$messagediv.append($('<li>').text(element.by+' : '+element.message));
}, this);
}
}
xhr.onerror = function(){
console.log("Request Error");
}
//sends request
xhr.send();
// END of XHR Request
var socket = io();
var $messageinput = $('#m');
var $messagediv = $('#messages');
var $connectionsdiv = $('#connections')
// console.log(socket);
$('#nform').submit(function(e){
e.preventDefault();
socket.emit('new user', $('#n').val());
$('#n').val('');
$('.nameform').hide();
$('.mainwrap').show();
return false;
});
socket.on('active users',function(users){
// console.log(users);
var html = '';
users.forEach(function(element) {
html += element+'<br />'
}, this);
$('#activeUsers').html(html);
})
socket.on('user count',function(count){
$connectionsdiv.html('<h1 style="text-align:center">Active Connections : '+count+'</h1>');
})
$('#mform').submit(function(){
socket.emit('chat message', $messageinput.val());
$messageinput.val('');
return false;
});
socket.on('chat message', function(data){
$messagediv.prepend($('<li>').text(data.name+' : '+data.msg));
});
});
</script>
</body>
</html>