-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatroom.html
172 lines (152 loc) · 5.19 KB
/
chatroom.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<link href="https://fonts.googleapis.com/css?family=Quicksand:300,400,500,700" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html {
font-family: 'Quicksand', sans-serif;
}
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { height:87vh; width: 90%; list-style-type: none; margin: 0; padding: 0; overflow: auto;}
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
#users {max-width: 10%; border-left: 2px solid; position:absolute; top:0; right:0; bottom: 0;width: 100%; list-style-type: none;background-color:white;z-index:1000;overflow: auto;}
#users li { padding: 5px; color: black; font-weight: bolder;}
form{
z-index: 10000;
}
</style>
</head>
<body>
<ul id="messages"></ul>
<ul id="users"></ul>
<form action="">
<input id="m" autocomplete="off" placeholder="Write something secret..." autofocus/><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 type="text/javascript" src="/aes.js"></script>
<script>
$(function () {
var socket = io();
var room = getRoomName();
var userName = getUsername();
socket.on('connect', function() {
if(!userName)
location.href = '/';
socket.emit('join room', room, userName);
});
socket.on('chat message', function(data){
var msg = decryptMsg(data.msg);
if(msg === false)
return;
if(data.userName === userName) {
$('#messages').append('<li><b>You</b> : '+msg);
} else {
$('#messages').append('<li><b>'+data.userName+'</b> : '+msg);
}
scrollDown();
});
socket.on('room users', function(users){
//console.log("getting room users");
$('#users').html('');
$.each(users, function(index, user){
$('#users').append('<li>'+user+'</li>');
});
});
socket.on('disconnect', (reason) => {
sessionStorage.clear();
window.location = '/';
//console.log("you're disconnected", reason);
});
socket.on('username exists', () => {
//console.log("username exists");
sessionStorage.clear();
window.location = '/?error=usernameexists';
});
socket.on('user joined', function(name){
if(userName === name) {
return;
}
//console.log("getting room users");
$('#messages').append("<li><b>"+name+"</b> joined this room </li>");
});
socket.on('user left', function(name){
console.log("getting room users");
$('#messages').append("<li><b>"+name+"</b> left this room </li>");
});
$('form').submit(function(){
var data = {
userName : getUsername(),
msg : encryptMsg($('#m').val())
}
socket.emit('chat message', data);
$('#m').val('');
return false;
});
/* functions */
function getRoomName() {
return window.location.pathname.split('/').slice(-1)[0];
}
//get current room username or redirect to join room
function getUsername()
{
var roomValue = roomExists();
if(
!roomValue ||
!roomValue.userName ||
roomValue.userName.trim() === ''
) {
return false;
}
return roomValue.userName;
}
function roomExists()
{
var rooms = sessionStorage.getItem('rooms');
if(!rooms) {
return false;
}
rooms = JSON.parse(rooms);
var room = false;
$.each(rooms, function(name, value){
if(name === getRoomName())
room = value;
});
return room;
}
function encryptMsg(plaintext) {
//console.log("encrypt", plaintext, getPassword());
return AesCtr.encrypt(plaintext, getPassword(), 256);
}
function decryptMsg(ciphertext) {
//console.log("decrypt", ciphertext, getPassword());
try {
return AesCtr.decrypt(ciphertext, getPassword(), 256);
} catch(e) {
return false;
}
}
function getPassword() {
var roomValue = roomExists();
if(
!roomValue ||
!roomValue.password ||
roomValue.password.trim().length === 0
) {
return false;
}
return roomValue.password;
}
function scrollDown() {
$("#messages").scrollTop($("#messages").prop('scrollHeight'));
}
});
</script>
</body>
</html>