-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
109 lines (99 loc) · 3.87 KB
/
index.html
File metadata and controls
109 lines (99 loc) · 3.87 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
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
<!doctype html>
<html>
<head>
<title>Node Chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; background: #2F3136; }
input {background: #484C52;}
button {background: #7F8186;}
h2, a {color:#FFFFFF; font-size:3vh;}
#head {position: fixed; width: 100%; height: 20px; padding: 3px; top:0; padding-left: 10px; padding-right: 10px;}
#usercount {float:right;}
form#userinfo { background: #202225; padding: 3px; padding-left: 0; padding-top: 3px; position: fixed; bottom: 0; right:0; width: 10%; }
form#userinfo input { border: 0; padding: 2.25vh; width: 100%; }
form#message { background: #202225; padding: 3px; position: fixed; bottom: 0; width: 90%; }
form#message input { border: 0; padding: 2.25vh; width: 90%; margin-right: .5%; }
form#message button { height: 6vh; width: 9%; border: none; padding: 10px; }
#messages {top: 30px; position: fixed; width:100%; height: 87%; overflow-y: auto;}
#messages ul { list-style-type: none; margin: 0; padding: 0; }
#messages ul li { padding: 5px 10px; line-height:2vh; font-style:3vh; color: #CCDADE; background: #34363C;}
#messages ul li:nth-child(odd) { background: #36393F; }
</style>
</head>
<body>
<div id="head">
<h2><a>NodeJS Chat By: Erik Nilsson</a><a id="usercount"></a></h2>
</div>
<form action="" id="userinfo">
<input id="user"/>
</form>
<div id="messages">
<ul></ul>
</div>
<form action="" id="message">
<input id="m" autocomplete="off" />
<button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function() {
var socket = io();
msgCount = 1;
socket.on('connect', function(){
socket.emit('newConnection');
});
socket.on('update', function(c){
$('#usercount').text(c + ' Online');
if (getCookie('username') == ""){
$('#user').val('Guest');
} else {
$('#user').val(getCookie('username'));
}
});
$('form').submit(function(e){
var date = new Date();
var time = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
e.preventDefault(); // prevents page reloading
if ($('#m').val()[0] == "/"){
socket.emit('command', $('#m').val().substring(1));
} else {
if ($('#m').val() != "") {
socket.emit('chat message', time + ' ' + $('#user').val() + ": " + $('#m').val());
}
}
$('#m').val('');
createCookie('username',$('#user').val(),1000);
return false;
});
socket.on('message', function(msg){
msgCount += 1;
$('#messages ul').append($('<li id="' + msgCount + '">').text(msg));
document.getElementById('messages').scrollBy(0,100);
});
socket.on('reload', function(){
location.reload(true);
});
function createCookie(cookieName,cookieValue,daysToExpire)
{
var date = new Date();
date.setTime(date.getTime()+(daysToExpire*24*60*60*1000));
document.cookie = cookieName + "=" + cookieValue + "; expires=" + date.toGMTString();
}
function getCookie(cookieName)
{
var name = cookieName + "=";
var allCookieArray = document.cookie.split(';');
for(var i=0; i<allCookieArray.length; i++)
{
var temp = allCookieArray[i].trim();
if (temp.indexOf(name)==0)
return temp.substring(name.length,temp.length);
}
return "";
}
});
</script>
</body>
</html>