Skip to content

Commit 82a6715

Browse files
committed
first commit
0 parents  commit 82a6715

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

index.css

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
text-align: center;
5+
}
6+
7+
.header {
8+
font-family: cursive;
9+
margin: 0;
10+
padding: 6px;
11+
}
12+
13+
.messages {
14+
display: inline-block;
15+
padding-bottom: 30px; /* 24 + 6 px */
16+
text-align: left;
17+
}
18+
19+
.message {
20+
margin: 6px;
21+
}
22+
23+
.message .avatar {
24+
float: left;
25+
width: 36px;
26+
height: 36px;
27+
background-size: 100%;
28+
background-repeat: no-repeat;
29+
border-radius: 18px;
30+
margin-top: 4px;
31+
}
32+
33+
.message .body {
34+
overflow: hidden;
35+
position: relative;
36+
margin-left: 42px;
37+
}
38+
39+
.message .text {
40+
display: inline-block;
41+
padding: 6px;
42+
background-color: #b5f0ff;
43+
border-radius: 24px;
44+
}
45+
46+
.message .text:before, .message .text:after {
47+
content: " ";
48+
}
49+
50+
.message .headline {
51+
font-size: 9pt;
52+
}
53+
54+
.message .name {
55+
font-weight: bold;
56+
}
57+
58+
.input {
59+
display: block;
60+
position: fixed;
61+
bottom: 6px;
62+
left: 6px;
63+
right: 6px;
64+
width: calc(100% - 12px);
65+
box-sizing: border-box;
66+
background: #eee;
67+
padding: 3px 6px;
68+
border: 1px solid #ccc;
69+
border-radius: 12px;
70+
height: 24px;
71+
line-height: 24px;
72+
font: inherit;
73+
outline: none;
74+
}

index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Chat Demo</title>
6+
<meta name="author" content="https://nikita.tk">
7+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
8+
<link type="text/css" rel="stylesheet" href="index.css"/>
9+
<script type="text/javascript" src="index.js"></script>
10+
</head>
11+
<body>
12+
<h1 class="header">Fun Chat</h1>
13+
<div class="messages"></div>
14+
<input class="input" type="text"/>
15+
</body>
16+
</html>

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const host = `ws://${ location.hostname }:57772`;
2+
const myName = "Nikita";
3+
const myAvatar = "http://nikita.tk/img/avatar.jpg";
4+
5+
let URL = host + "/csp/user/WebCourse.ChatWebSocket.cls",
6+
ws = new WebSocket(URL);
7+
8+
ws.addEventListener("open", () => ws.send(JSON.stringify({
9+
name: myName,
10+
avatar: myAvatar
11+
})));
12+
13+
ws.addEventListener("error", (err) => printMessage({
14+
name: "System",
15+
text: "Connection error: " + err.toString()
16+
}));
17+
18+
ws.addEventListener("close", () => printMessage({
19+
name: "System",
20+
text: "We're out!"
21+
}));
22+
23+
ws.addEventListener("message", (m) => {
24+
let message = JSON.parse(m.data);
25+
if (message["error"])
26+
return console.error(`Server reporting an error: ${ message.error }`);
27+
if (message["updates"] instanceof Array) message["updates"].forEach(update => {
28+
if (update.type === "message")
29+
printMessage(update);
30+
else if (update.type === "notification")
31+
printMessage(update);
32+
else
33+
console.warn("Unhandled WebSocket message", message);
34+
});
35+
});
36+
37+
function printMessage ({ date = Date.now(), name, text, avatar = "" }) {
38+
let block = document.querySelector(".messages");
39+
block.innerHTML += `<div class="message">
40+
<div class="avatar" style="background-image: url(${ avatar })"></div>
41+
<div class="body">
42+
<div class="headline">
43+
<span class="date">${ new Date(date).toLocaleString() }</span>,
44+
<span class="name">${ name }</span>
45+
</div>
46+
<div class="text">${ text }</div>
47+
</div>
48+
</div>`;
49+
document.body.scrollTop = 99999999;
50+
}
51+
52+
document.addEventListener("DOMContentLoaded", () => {
53+
const input = document.querySelector(".input");
54+
input.addEventListener("keydown", (event) => {
55+
if (input.value && event.keyCode === 13) {
56+
ws.send(JSON.stringify({ "text": input.value }));
57+
input.value = "";
58+
}
59+
});
60+
});

0 commit comments

Comments
 (0)