-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (120 loc) · 4.35 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
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>
<meta charset="UTF-8">
<title>WebUp Chat</title>
</head>
<body>
<div class="container">
<div id="list" class="list"></div>
<div class="input">
<div class="input-name">
<input type="text" id="input-name" placeholder="Username">
</div>
<div class="input-content">
<input type="text" id="input-content" placeholder="Write a message...">
</div>
</div>
</div>
<script type="text/javascript">
var ws = new WebSocket("ws://" + location.host + "/socket");
ws.addEventListener("open", function () {
append("SYSTEM", "Connection opened");
});
ws.addEventListener("close", function () {
append("SYSTEM", "Connection closed");
});
ws.addEventListener("error", function () {
append("SYSTEM", "WebSocket Error");
});
ws.addEventListener("message", function (event) {
var message = JSON.parse(event.data);
append(message.name, message.content);
});
var $inputName = document.getElementById("input-name");
var $inputContent = document.getElementById("input-content");
$inputContent.addEventListener("keydown", function (event) {
if (event.code === "Enter") {
ws.send(JSON.stringify({
name: $inputName.value,
content: $inputContent.value,
}));
$inputContent.value = "";
}
});
function append(name, content) {
var $name = document.createElement("DIV");
$name.className = "message-name";
$name.textContent = name + ":\u00A0";
var $content = document.createElement("DIV");
$content.className = "message-content";
$content.textContent = content;
var $message = document.createElement("DIV");
$message.className = "message";
$message.appendChild($name);
$message.appendChild($content);
var $root = document.getElementById("list");
if ($root.childNodes.length > 0) {
$root.insertBefore($message, $root.childNodes.item(0));
} else {
$root.appendChild($message);
}
}
</script>
<style type="text/css">
html, body {
border: 0 none;
overflow: hidden;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
.container {
display: flex;
flex-flow: column nowrap;
width: 100%;
height: 100%;
}
.list {
flex: 1 1;
min-height: 200px;
overflow: auto;
box-sizing: border-box;
padding: 15px;
}
.input {
flex: 0 0 60px;
display: flex;
flex-flow: row nowrap;
box-sizing: border-box;
padding: 15px 10px;
border-top: 1px solid black;
}
.input-name {
flex: 0 0 150px;
box-sizing: border-box;
padding: 0 5px;
}
.input-name input,
.input-content input {
width: 100%;
}
.input-content {
flex: 1 1;
box-sizing: border-box;
padding: 0 5px;
}
.message {
display: flex;
flex-flow: row nowrap;
align-items: flex-start;
box-sizing: border-box;
padding: 5px 0;
}
.message-name {
font-weight: bold;
}
</style>
</body>
</html>