-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
148 lines (137 loc) · 4.88 KB
/
index.html
File metadata and controls
148 lines (137 loc) · 4.88 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="top">Chat Application</div>
<div id="getNick">
<form name="usernameForm"><input type="text" id="n" autocomplete="off" placeholder="Enter your nickname here"/>
<button>Confirm</button>
</form>
</div>
<div id="main"><div class="info" id="username"></div>
<div class="LRcontain">
<div id="left">
<ul id="messages"></ul>
</div>
<div id="right">
<div class="info"><h4>Current Users</h4></div>
<ul id="users"></ul>
</div>
</div>
<div class="input">
<form name="chatForm"><input type="text" id="m" autocomplete="off" placeholder="Your message here"/>
<button>Send</button>
</form>
</div>
</div>
<div id="foot"><h4>Application Produced by Austin Wattling</h4></div>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
let self_username = "x";
let self_color = "#6A5ACD";
$(function () {
var socket = io();
let preName = getCookie('preName');
if(preName != ''){
socket.emit('newuser', preName, function (data){
if(data){
// yay username not being used
self_username = preName;
setCookie('preName', self_username, 30);
$('#getNick').hide();
$('#main').show();
}
else{
self_username = "x";
}
});
}
$('form').submit(function(){
// Catches the username form //
if(self_username === "x"){
socket.emit('newuser', $('#n').val(), function(data){
if(data) {
self_username = $('#n').val();
setCookie('preName', self_username, 30);
$('#getNick').hide();
$('#main').show();
}
else{
$('#getNick').append("Username already in use. Please try again");
// Should print error for user // Nickname invalid //
}
});
}// Catches the chat form submission//
else{
socket.emit('chat message', $('#m').val(), self_username, self_color);
$('#m').val('');
}
return false;
});
// Handles the receipt of a message from the server//;
socket.on('chat message', function(msg){
let value = "<li>";
let date = new Date(msg.timestamp);
value += date.getHours() + ":";
value += date.getMinutes()+ " ";
value += "<span style='color:";
value += msg.color + "'>";
value += msg.user + "</span>: ";
if(msg.user === self_username){
value += "<strong>" + msg.content + "</strong>";
}
else{
value += msg.content;
}
value += "</li>"
// Adds msg to the running html list //
$('#messages').append(value);
$('#left').scrollTop($('#messages').height());
});
// Handles this as a client //
socket.on('createUser', function (username) {
self_username = username;
//setCookie('preName', self_username, 30);
$("#username").html("<h4>Your username is " + self_username + "</h4>");
});
socket.on('changeColor', function (color) {
self_color = color;
console.log("Color changed");
});
// Handles the creation of another user //
socket.on('updateUsers', function (usernames) {
let html = "";
for(let y in usernames){
html += "<li>" + usernames[y];
}
$('#users').html(html);
});
function setCookie(cname, cvalue, exdays) {
let d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
let expires = 'expires=' + d.toUTCString();
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
function getCookie(cname) {
let name = cname + '=';
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
});
</script>
</body>
</html>