2017-04-18 07:31:46 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const io = require("socket.io-client");
|
2017-09-10 15:28:28 +00:00
|
|
|
const utils = require("./utils");
|
2017-04-18 07:31:46 +00:00
|
|
|
|
|
|
|
const socket = io({
|
2017-06-01 19:43:23 +00:00
|
|
|
transports: $(document.body).data("transports"),
|
2018-03-05 00:59:16 +00:00
|
|
|
path: window.location.pathname + "socket.io/",
|
2017-04-18 07:31:46 +00:00
|
|
|
autoConnect: false,
|
2017-11-15 06:35:15 +00:00
|
|
|
reconnection: !$(document.body).hasClass("public"),
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
2018-09-03 07:30:05 +00:00
|
|
|
$("#connection-error").on("click", function() {
|
|
|
|
$(this).removeClass("shown");
|
|
|
|
});
|
|
|
|
|
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) {
|
2017-11-27 17:39:16 +00:00
|
|
|
$("#loading-page-message, #connection-error").text(`Reconnecting… (attempt ${attempt})`);
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connecting", function() {
|
2017-11-27 17:39:16 +00:00
|
|
|
$("#loading-page-message, #connection-error").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 = [];
|
|
|
|
|
2017-11-27 17:39:16 +00:00
|
|
|
$("#loading-page-message, #connection-error").text("Finalizing connection…");
|
2017-04-18 07:31:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("authorized", function() {
|
2017-11-27 17:39:16 +00:00
|
|
|
$("#loading-page-message, #connection-error").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;
|
|
|
|
|
2017-11-27 17:39:16 +00:00
|
|
|
$("#loading-page-message, #connection-error").text(`Waiting to reconnect… (${message})`).addClass("shown");
|
2018-04-15 03:02:35 +00:00
|
|
|
$(".show-more button, #input").prop("disabled", true);
|
2017-08-28 15:03:27 +00:00
|
|
|
$("#submit").hide();
|
2018-09-03 07:30:05 +00:00
|
|
|
$("#upload").hide();
|
2017-09-10 15:28:28 +00:00
|
|
|
|
|
|
|
// If the server shuts down, socket.io skips reconnection
|
|
|
|
// and we have to manually call connect to start the process
|
|
|
|
if (socket.io.skipReconnect) {
|
|
|
|
utils.requestIdleCallback(() => socket.connect(), 2000);
|
|
|
|
}
|
2017-08-28 15:03:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 07:31:46 +00:00
|
|
|
module.exports = socket;
|