-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.html
More file actions
144 lines (141 loc) · 3.27 KB
/
home.html
File metadata and controls
144 lines (141 loc) · 3.27 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Go Chat</title>
<script type="text/javascript">
window.onload = function () {
var conn;
var from, to;
var msg = document.getElementById("msg");
var log = document.getElementById("log");
var clients = document.getElementById("clients");
function appendLog(item) {
var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
log.appendChild(item);
if (doScroll) {
log.scrollTop = log.scrollHeight - log.clientHeight;
}
}
function appendClient(name){
var item = document.createElement("button");
item.addEventListener("click", function () {
to = this.innerText;
});
item.innerText = name;
clients.appendChild(item);
}
document.getElementById("form").onsubmit = function () {
if (!conn) {
return false;
}
if (!msg.value) {
return false;
}
conn.send("FROM:"+from+"\n"+"TO:"+to+"\n"+"MESSAGE:"+msg.value);
msg.value = "";
return false;
};
//debugger;
if (window["WebSocket"]) {
conn = new WebSocket("ws://" + document.location.host + "/ws");
conn.onclose = function (evt) {
var item = document.createElement("div");
item.innerHTML = "<b>Connection closed.</b>";
appendLog(item);
};
conn.onmessage = function (evt) {
var messages = evt.data.split('\n');
for (var i = 0; i < messages.length; i++) {
if(messages[i].indexOf("NAME:") !== -1){
appendClient(messages[i].substr(5));
}
else {
var item = document.createElement("div");
console.log(messages);
item.innerText = messages[i].substr(messages[i].indexOf("MESSAGE:")+8);
appendLog(item);
}
}
};
conn.onopen = function(e){
from = prompt("Please enter your display name:");
if(from !== null){
conn.send("NAME:"+ from);
}
}
} else {
var item = document.createElement("div");
item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
appendLog(item);
}
};
</script>
<style type="text/css">
html {
overflow: hidden;
}
body {
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
background: gray;
}
#log {
background: white;
margin: 0;
padding: 0.5em 0.5em 0.5em 0.5em;
position: absolute;
top: 0.5em;
left: 0.5em;
right: 0.5em;
bottom: 3em;
width: 60%;
overflow: auto;
}
#clients {
background: white;
margin: 0;
position: absolute;
padding: 0.5em 0.5em 0.5em 0.5em;
top: 0.5em;
right: 0.5em;
bottom: 3em;
width: 30%;
overflow: auto;
}
button {
margin: 10px 10px 10px 10px;
height: 21px;
}
#form {
padding: 0 0.5em 0 0.5em;
margin: 0;
position: absolute;
bottom: 1em;
left: 0px;
width: 100%;
overflow: hidden;
}
.title{
padding: 20px;
margin: 0px;
background: black;
color: white;
}
</style>
</head>
<body>
<div id="log">
<h4 class="title"> Go Chat </h4>
</div>
<div id="clients">
<h4 class="title"> Active Members</h4>
</div>
<form id="form">
<input type="submit" value="Send" />
<input type="text" id="msg" size="64"/>
</form>
</body>
</html>