2017-04-18 03:31:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const io = require("socket.io-client");
|
|
|
|
const path = window.location.pathname + "socket.io/";
|
|
|
|
|
|
|
|
const socket = io({
|
2017-06-01 15:43:23 -04:00
|
|
|
transports: $(document.body).data("transports"),
|
2017-04-18 03:31:46 -04:00
|
|
|
path: path,
|
|
|
|
autoConnect: false,
|
2017-08-28 05:18:31 -04:00
|
|
|
reconnection: !$(document.body).hasClass("public")
|
2017-04-18 03:31:46 -04:00
|
|
|
});
|
|
|
|
|
2017-08-28 05:18:31 -04:00
|
|
|
window.lounge_socket = socket; // TODO: Remove later, this is for debugging
|
|
|
|
|
2017-04-18 03:31:46 -04:00
|
|
|
[
|
|
|
|
"connect_error",
|
|
|
|
"connect_failed",
|
|
|
|
"disconnect",
|
|
|
|
"error",
|
|
|
|
].forEach(function(e) {
|
|
|
|
socket.on(e, function(data) {
|
|
|
|
$("#loading-page-message").text("Connection failed: " + data);
|
|
|
|
$("#connection-error").addClass("shown").one("click", function() {
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Disables sending a message by pressing Enter. `off` is necessary to
|
|
|
|
// cancel `inputhistory`, which overrides hitting Enter. `on` is then
|
|
|
|
// necessary to avoid creating new lines when hitting Enter without Shift.
|
|
|
|
// This is fairly hacky but this solution is not permanent.
|
|
|
|
$("#input").off("keydown").on("keydown", function(event) {
|
|
|
|
if (event.which === 13 && !event.shiftKey) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Hides the "Send Message" button
|
2017-08-28 05:18:31 -04:00
|
|
|
$("#submit").hide();
|
2017-04-18 03:31:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connecting", function() {
|
|
|
|
$("#loading-page-message").text("Connecting…");
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connect", function() {
|
2017-08-28 05:18:31 -04: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 03:31:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("authorized", function() {
|
2017-08-28 05:18:31 -04:00
|
|
|
$("#loading-page-message").text("Loading messages…");
|
2017-04-18 03:31:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = socket;
|