- ws.addEventListener("message", ws_receive);
- ws.addEventListener("close", function (event) {
- event = event || window.event;
- new Message({"text": "Closed", "user": "ws_close"}, null);
- // display reconnect button here
- });
- ws.addEventListener("error", function (event) {
- event = event || window.event;
- console.log(event);
- new Message({"text": event, "user": "ws_error"}, null);
- // display reconnect button here
- });
+ function connect() {
+ var ws = new WebSocket(
+ ws_schemas[window.location.protocol]
+ + "//"
+ + window.location.host
+ + "/"
+ );
+ ws.addEventListener("message", ws_receive);
+ ws.addEventListener("close", function () {
+ messages_manager.stick_to_bottom();
+ new Message({"text": "Closed", "user": "ws_close"}, null);
+ messages_manager.bottom_callback();
+ toggle_reconnect_button();
+ });
+ ws.addEventListener("error", function (event) {
+ event = event || window.event;
+ console.log(event);
+ messages_manager.stick_to_bottom();
+ new Message({"text": event, "user": "ws_error"}, null);
+ messages_manager.bottom_callback();
+ toggle_reconnect_button();
+ });
+ return ws;
+ }
+ function try_reconnect() {
+ var messages_footer = _.dgEBCN0("messages_footer");
+ var ws;
+ if (messages_footer.children.length <= 2) {
+ return;
+ }
+ messages_footer.children[2].setAttribute("disabled", "");
+ ws = connect();
+ ws.addEventListener("open", function () {
+ var prev_channel;
+ var prev_id;
+ if (
+ current_channel !== null
+ && current_channel.hasOwnProperty("msg_data")
+ && current_channel.hasOwnProperty("msg_dest_field")
+ ) {
+ prev_channel = current_channel;
+ prev_id = current_channel.msg_data[
+ current_channel.msg_dest_field
+ ];
+ } else {
+ prev_channel = window;
+ prev_id = null;
+ }
+ toggle_reconnect_button();
+ private_manager.items_reload(function () {
+ channel_manager.items_reload(function () {
+ set_channel.call(prev_channel, prev_id);
+ });
+ });
+ });
+ function reenable_reconnect() {
+ if (messages_footer.children > 2) {
+ messages_footer.children[2].removeAttribute("disabled");
+ }
+ }
+ ws.addEventListener("close", reenable_reconnect);
+ ws.addEventListener("error", reenable_reconnect);
+ }
+ function toggle_reconnect_button() {
+ var messages_footer = _.dgEBCN0("messages_footer");
+ if (messages_footer.children[0].style.display === "none") {
+ messages_footer.children[0].style.display = "";
+ messages_footer.children[1].style.display = "";
+ _.clear_after(messages_footer.children[1]);
+ } else {
+ messages_footer.children[0].style.display = "none";
+ messages_footer.children[1].style.display = "none";
+ messages_footer.appendChild(document.createElement("button"));
+ messages_footer.children[2].appendChild(_.dcTN("Reconnect"));
+ messages_footer.children[2].style.width = "100%";
+ messages_footer.children[2].addEventListener(
+ "click",
+ try_reconnect
+ );
+ }
+ }
+ connect();