2017-04-18 07:31:46 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const io = require("socket.io-client");
|
|
|
|
const path = window.location.pathname + "socket.io/";
|
2017-08-28 15:03:27 +00:00
|
|
|
const status = $("#loading-page-message, #connection-error");
|
2017-04-18 07:31:46 +00:00
|
|
|
|
|
|
|
const socket = io({
|
2017-06-01 19:43:23 +00:00
|
|
|
transports: $(document.body).data("transports"),
|
2017-04-18 07:31:46 +00:00
|
|
|
path: path,
|
|
|
|
autoConnect: false,
|
2017-08-28 09:18:31 +00:00
|
|
|
reconnection: !$(document.body).hasClass("public")
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
socket.on("disconnect", handleDisconnect);
|
|
|
|
socket.on("connect_error", handleDisconnect);
|
|
|
|
socket.on("error", handleDisconnect);
|
|
|
|
|
|
|
|
socket.on("reconnecting", function(attempt) {
|
|
|
|
status.text(`Reconnecting… (attempt ${attempt})`);
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connecting", function() {
|
2017-08-28 15:03:27 +00:00
|
|
|
status.text("Connecting…");
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connect", function() {
|
2017-08-28 09:18:31 +00:00
|
|
|
// Clear send buffer when reconnecting, socket.io would emit these
|
|
|
|
// immediately upon connection and it will have no effect, so we ensure
|
|
|
|
// nothing is sent to the server that might have happened.
|
|
|
|
socket.sendBuffer = [];
|
|
|
|
|
|
|
|
status.text("Finalizing connection…");
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("authorized", function() {
|
2017-08-28 15:03:27 +00:00
|
|
|
status.text("Loading messages…");
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
function handleDisconnect(data) {
|
|
|
|
const message = data.message || data;
|
|
|
|
|
|
|
|
status.text(`Waiting to reconnect… (${message})`).addClass("shown");
|
|
|
|
$(".show-more-button, #input").prop("disabled", true);
|
|
|
|
$("#submit").hide();
|
|
|
|
}
|
|
|
|
|
2017-04-18 07:31:46 +00:00
|
|
|
module.exports = socket;
|