]>
git.mar77i.info Git - hublib/blob - hubapps/first/index.js
f6fa9c7367a62c371053a2f9d74a03ddc03f3cfd
6 common
.write(input_div
.previousSibling
, msg
);
9 function change_name_button(hub
) {
10 var btn
= document
.createElement("button");
11 btn
.append(document
.createTextNode("change name"));
12 btn
.addEventListener("click", function () {
13 setup_change_name(hub
);
18 function setup_chat(hub
) {
19 input_div
= document
.createElement("div");
20 input_div
.append(common
.get_input(function () {
21 var input
= input_div
.getElementsByTagName("input")[0];
22 hub
.send(input
.value
);
25 input_div
.append(document
.createTextNode(" "));
26 input_div
.append(change_name_button(hub
));
28 document
.body
.append(document
.createElement("div"));
29 document
.body
.append(input_div
);
32 function setup_change_name(hub
) {
33 var div
= document
.createElement("div");
34 div
.append(document
.createTextNode("Enter name: "));
35 div
.append(common
.get_input(function () {
36 var client_name
= div
.children
[0].value
;
37 if (input_div
.childNodes
[0] !== input_div
.children
[0]) {
38 input_div
.childNodes
[0].remove();
40 input_div
.insertBefore(
41 document
.createTextNode(client_name
+ " "), input_div
.children
[0]
43 hub
.send(JSON
.stringify({"action": "set_name", "name": client_name
}));
45 input_div
.style
.display
= "";
46 input_div
.children
[0].focus();
48 document
.body
.append(div
);
49 input_div
.style
.display
= "none";
50 div
.children
[0].focus();
55 write("connected to " + ws_uri
);
56 setup_change_name(this);
60 write("connection lost");
63 function message(msg
) {
64 var obj
= JSON
.parse(msg
.data
);
65 if (obj
.action
=== "join" || obj
.action
=== "leave") {
66 write("[action]: " + obj
.name
+ " " + obj
.action
+ "s");
67 } else if (obj
.action
== "set_name") {
74 } else if (obj
.hasOwnProperty("data")) {
75 write(obj
.name
+ ": " + obj
.data
);
79 common
.setup(function () {new common
.HubClient(ws_uri
, open
, close
, message
)});