-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
197 lines (190 loc) · 7.27 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<title>IPv8 Chat</title>
<style type="text/css">
html {
height: 100%;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
body {
font-family: "Courier New", sans-serif;
text-align: left;
height: 100%;
width: 100%;
}
input {
font-family: "Courier New", sans-serif;
text-align: left;
font-size: 1em;
color: blue;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
}
p {
margin: 0;
}
img {
image-rendering: pixelated;
image-rendering: -moz-crisp-edges;
image-rendering: crisp-edges;
}
.chatmessage {
display: flex;
flex-direction: row;
align-items: center;
padding-left: 10px;
word-break: break-all;
padding-top: 5px;
padding-bottom: 0px;
}
.fstable {
height:100%;
width:100%;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
border-spacing: 0;
}
.state {
font-size: 2em;
width:100%;
}
.button {
font-family: "Courier New", sans-serif;
font-size: 1em;
color: black;
font-weight: bold;
position: relative;
right: 0;
}
.chatlog {
border: 1px solid black;
font: "Courier New", sans-serif;
overflow-x: hidden;
overflow-y: scroll;
height: 100%;
}
.community {
font-size: 1em;
width: 100%;
}
.newmessagetext {
font-size: 2em;
border: 0;
outline-width: 0;
width: 100%;
}
.inputbox {
width: 100%;
}
</style>
</head>
<body>
<table class="fstable">
<tr style="width: 100%;"><td style="width: 100%">
<table class="state">
<tr>
<td>Connections:</td><td style="width: 100%"><span class="users">?</span></td><td></td>
</tr><tr>
<td>Room:</td><td style="width: 100%;"><input class="community" maxlength="40" value="00697076386368617467656e6572616c63686174"></td><td><input class="button" id="submitroom" type="submit" value="Join"></td>
</tr>
</table>
</td></tr>
<tr style="width: 100%; height: 100%;"><td style="margin: 10px;">
<div class="chatlog">
<ul style="height:0;" class="chatloglist">
</ul>
</div>
</td></tr>
<tr style="width: 100%;"><td style="width: 100%;">
<table class="inputbox">
<tr style="width: 100%">
<td>></td><td style="width:100%;"><input class="newmessagetext" maxlength="250"></td><td><input class="button" id="submittext" type="submit" value="Submit" style="font-size: 2em;"></td>
</tr>
</table>
</td></tr>
</table>
<script>
var submitroombutton = document.getElementById('submitroom'),
submittextbutton = document.getElementById('submittext'),
newmessagetext = document.querySelector('.newmessagetext');
const params = new URLSearchParams(document.location.search);
const wsport = params.get("wsport");
var ws_url = "ws://127.0.0.1:" + wsport + "/"
var websocket = new WebSocket(ws_url);
var failed = false;
function endOfStream(event){
if (!failed){
failed = true;
document.body.innerHTML = '';
document.write("Failed to connect to " + ws_url + "<br>Please start your background service and reload this page!");
window.stop();
}
}
websocket.onerror = endOfStream
websocket.onclose = endOfStream
submitroombutton.onclick = function (event) {
websocket.send(JSON.stringify({action: 'join', communityid: document.querySelector('.community').value}));
}
submittextbutton.onclick = function (event) {
websocket.send(JSON.stringify({action: 'send', message: newmessagetext.value}));
newmessagetext.value = "";
newmessagetext.focus();
}
newmessagetext.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
submittextbutton.click();
}
});
function addChatMessage(imgdata, message){
var chatloglist = document.querySelector('.chatloglist')
var chatmessage = document.createElement("li");
var userimg = document.createElement("img");
var imgsrc = "data:image/png;base64," + imgdata
var chattext = document.createElement("p");
var spacer = document.createTextNode("\xa0");
chattext.textContent = message
userimg.setAttribute("width", "32px");
userimg.setAttribute("height", "32px");
userimg.setAttribute("src", imgsrc);
chatmessage.setAttribute("class", "chatmessage");
chatmessage.appendChild(userimg);
chatmessage.appendChild(spacer);
chatmessage.appendChild(chattext);
chatloglist.appendChild(chatmessage);
}
websocket.onmessage = function (event) {
data = JSON.parse(event.data);
switch (data.type) {
case 'receive':
addChatMessage(data.img, data.message);
break;
case 'users':
var users = document.querySelector('.users');
users.textContent = (
data.count.toString() + " connection" +
(data.count == 1 ? "" : "s"));
break;
default:
console.error(
"unsupported event", data);
}
};
websocket.onopen = function(event){
// Finalize
submitroombutton.click();
newmessagetext.focus();
}
</script>
</body>
</html>