-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
101 lines (91 loc) · 3.31 KB
/
Copy pathindex.html
File metadata and controls
101 lines (91 loc) · 3.31 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
// Basic functionality from http://code.runnable.com/UqDMKY-VwoAMABDk/simple-websockets-chat-with-tornado-for-python
var ws;
var username;
function createChatEntry(username, message) {
var entry = document.createElement("div");
entry.class = "chat_entry";
var dom_uname = document.createElement("span");
dom_uname.class = "chat_username";
dom_uname.innerHTML = username+": ";
entry.appendChild(dom_uname);
var dom_msg = document.createElement("span");
dom_msg.class = "chat_message";
dom_msg.innerHTML = message;
entry.appendChild(dom_msg);
return entry;
}
function openWS(messageContainer) {
ws = new WebSocket("ws://localhost:8888/chat");
ws.onmessage = function(e) {
var d = new Date();
var data = JSON.parse(e.data);
messageContainer.appendChild(createChatEntry(data.user, data.message));
console.log(d.getTime() - data.time)
};
ws.onclose = function(e) {
openWS(messageContainer);
};
}
function sendMessage() {
var d = new Date();
var data = {
type: "message",
time: d.getTime(),
user: username,
message: document.getElementById("message").value};
if(username && data.message) {
ws.send(JSON.stringify(data));
}
}
function sendUser(data) {
if( ws.readyState == 1 ) {
ws.send(JSON.stringify(data));
} else {
setTimeout(function() { sendUser(data); }, 1000);
}
event.target.value = '';
}
window.onload = function() {
var messageContainer = document.getElementById("chat");
if("WebSocket" in window) {
messageContainer.appendChild(createChatEntry("[SYSTEM]", "WebSocket is supported by your browser!"));
messageContainer.appendChild(createChatEntry("[SYSTEM]", "Pick a username and start sending out messages."));
username = window.prompt("Enter your username, or leave blank if you wish to remain anonymous:");
openWS(messageContainer);
if( username != "" ) {
pass = window.prompt("Enter a password to save your session:")
data = { type: "username", user: username, password: pass };
sendUser(data);
} else {
username = "Anonymous";
data = { type: "username", user: "Anonymous",
password: "" };
sendUser(data);
}
document.getElementById('message').onkeyup = function(event) {
if (event.keyCode == 13) {
// If return is pressed, send the contents of the chatbox.
sendMessage();
event.target.value = ''; // Clear the chat box
}
}
}
else {
messageContainer.appendChild(createChatEntry("[SYSTEM]", "WebSocket is NOT supported by your browser!"));
}
}
</script>
</head>
<body>
<div id="chat" style="width: 100%; height: 40em; overflow: scroll; font-family: Arial"></div>
<div id="input_area">
<textarea id="message" placeholder="Your message" style="display: block; width: 400px"></textarea>
<button onclick="sendMessage()" style="display: block">Send</button>
</div>
</body>
</html>