-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
151 lines (130 loc) · 4.67 KB
/
client.js
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
var ws = new WebSocket("ws://localhost:5000");
var randomid = function(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
var nickname = "Anonymous."+randomid(5);
var channel = "public";
window.onload = function()
{
var url = location.search;
var params = parse_query_string();
nickname = (params["nickname"] === undefined ? nickname : params["nickname"]);
channel = (params["channel"] === undefined ? channel : params["channel"]);
}
ws.onopen = function(){
console.log("Server Connected!");
ws.send("("+nickname+"&"+channel+")__join__");
};
ws.onmessage = function(e){
elements = e.data.split("#$%^&");
elements.forEach(function(element){
bubble(element);
});
// bubble(e.data);
};
ws.onclose = function(e){
console.log("WebSocketClosed!");
bubble("Connection closed.");
};
ws.onerror = function(e){
console.log("Connection Error!");
bubble("Connection error.");
};
var send = function(text) {
ws.send("("+nickname+"&"+channel+")"+text);
}
var loading_cell = document.createElement("div");
var loading = function() { // show loading animation
if ( loading_cell.parentElement != undefined ) loading_cell.parentElement.style = "display:none";
var row = document.getElementById("history").insertRow();
loading_cell = row.insertCell();
loading_cell.innerHTML = "<div class=\"lds-ellipsis\"><div></div><div></div><div></div><div></div></div>";
to_bottom();
}
var bubble = function(message) {
// if the message is "loading", show loading animation
// if the message does not include ":", show the message as a notification
// if the message includes ":", then it as a conversation bubble, the substring before ":" is the username
if ( message == undefined || message.length == undefined ) return;
if ( message == "loading" ) {
loading();
return;
}
if ( loading_cell.parentElement != undefined ) loading_cell.parentElement.style = "display:none";
var row = document.getElementById("history").insertRow();
var cell = row.insertCell();
cell.innerHTML = bubble_content(message);
to_bottom();
}
var bubble_content = function(message) {
var i = message.indexOf(":");
if ( i < 0 )
return "<div class=\"notification\"><p>" + message + "</p></div>";
var username = message.substring(0,i);
var content = message.substring(i+1,message.length);
var i = content.indexOf(":");
var time = content.substring(0,i).split('-'); // YYYY-MM-DD-hh-mm-ss
var result = "";
if (username != "__you__") {
result = "<span style=\"font-size:10px;color:#999999;\">" + username + " "; // show username
} else {
result = "<span style=\"font-size:10px;color:#d9d9d9;\">"
}
result += time[3] + ":" + time[4] + "</span><br/>"; // show time hh:mm
result += content.substring(i+1,content.length); // show message
if (username == "__you__")
result = "<div class=\"right-arrow\"></div><div class=\"bubble-me\">" + result + "</div>";
else
result = "<div class=\"left-arrow\"></div><div class=\"bubble\">" + result + "</div>";
return result;
}
var click_send = function() {
var m = document.getElementById("message");
if ( m.value == "" ) return;
if ( m.value.length > 5000 ) {
alert("Your message is too long!");
return;
}
send(m.value);
m.value = "";
m.focus();
}
var to_bottom = function() {
var div = document.getElementById("history-container");
div.scrollTop = div.scrollHeight; // go to the bottom
}
var onKeyDown = function(e) {
e = e || window.event;
if (e.keyCode == 13 && e.shiftKey) {
return;
}
if (e.keyCode == 13) {
e.returnValue = false;
click_send();
}
}
function parse_query_string() {
var query = window.location.search.substring(1);
var vars = query.split("&");
var query_string = {};
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
var key = decodeURIComponent(pair[0]);
var value = decodeURIComponent(pair[1]);
if (typeof query_string[key] === "undefined") {
query_string[key] = decodeURIComponent(value);
} else if (typeof query_string[key] === "string") {
var arr = [query_string[key], decodeURIComponent(value)];
query_string[key] = arr;
} else {
query_string[key].push(decodeURIComponent(value));
}
}
return query_string;
}