Force reload the page if socket reconnects and server restarted

This commit is contained in:
Pavel Djundik 2017-08-28 12:18:31 +03:00
parent c7433eca95
commit 0c0df1efc9
5 changed files with 51 additions and 9 deletions

View File

@ -588,9 +588,6 @@ $(function() {
} }
setTimeout(updateDateMarkers, msUntilNextDay()); setTimeout(updateDateMarkers, msUntilNextDay());
// Only start opening socket.io connection after all events have been registered
socket.open();
window.addEventListener("popstate", (e) => { window.addEventListener("popstate", (e) => {
const {state} = e; const {state} = e;
if (!state) { if (!state) {
@ -604,4 +601,7 @@ $(function() {
}); });
} }
}); });
// Only start opening socket.io connection after all events have been registered
socket.open();
}); });

View File

@ -3,8 +3,19 @@
const $ = require("jquery"); const $ = require("jquery");
const socket = require("../socket"); const socket = require("../socket");
const storage = require("../localStorage"); const storage = require("../localStorage");
const utils = require("../utils");
socket.on("auth", function(data) { socket.on("auth", function(data) {
// If we reconnected and serverHash differents, that means the server restarted
// And we will reload the page to grab the latest version
if (utils.serverHash > -1 && data.serverHash > -1 && data.serverHash !== utils.serverHash) {
socket.disconnect();
location.reload(true);
return;
}
utils.serverHash = data.serverHash;
const login = $("#sign-in"); const login = $("#sign-in");
let token; let token;
const user = storage.get("user"); const user = storage.get("user");
@ -12,6 +23,13 @@ socket.on("auth", function(data) {
login.find(".btn").prop("disabled", false); login.find(".btn").prop("disabled", false);
if (!data.success) { if (!data.success) {
if (login.length === 0) {
socket.disconnect();
$("#connection-error").text("Authentication failed, reloading…");
location.reload();
return;
}
storage.remove("token"); storage.remove("token");
const error = login.find(".error"); const error = login.find(".error");
@ -20,9 +38,15 @@ socket.on("auth", function(data) {
}); });
} else if (user) { } else if (user) {
token = storage.get("token"); token = storage.get("token");
if (token) { if (token) {
$("#loading-page-message").text("Authorizing…"); $("#loading-page-message").text("Authorizing…");
socket.emit("auth", {user: user, token: token});
socket.emit("auth", {
user: user,
token: token,
lastMessage: utils.lastMessageId,
});
} }
} }

View File

@ -8,9 +8,11 @@ const socket = io({
transports: $(document.body).data("transports"), transports: $(document.body).data("transports"),
path: path, path: path,
autoConnect: false, autoConnect: false,
reconnection: false reconnection: !$(document.body).hasClass("public")
}); });
window.lounge_socket = socket; // TODO: Remove later, this is for debugging
[ [
"connect_error", "connect_error",
"connect_failed", "connect_failed",
@ -34,7 +36,7 @@ const socket = io({
} }
}); });
// Hides the "Send Message" button // Hides the "Send Message" button
$("#submit").remove(); $("#submit").hide();
}); });
}); });
@ -43,11 +45,16 @@ socket.on("connecting", function() {
}); });
socket.on("connect", function() { socket.on("connect", function() {
$("#loading-page-message").text("Finalizing connection…"); // 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…");
}); });
socket.on("authorized", function() { socket.on("authorized", function() {
$("#loading-page-message").text("Authorized, loading messages…"); $("#loading-page-message").text("Loading messages…");
}); });
module.exports = socket; module.exports = socket;

View File

@ -3,7 +3,12 @@
const $ = require("jquery"); const $ = require("jquery");
const input = $("#input"); const input = $("#input");
var serverHash = -1;
var lastMessageId = -1;
module.exports = { module.exports = {
serverHash,
lastMessageId,
confirmExit, confirmExit,
forceFocus, forceFocus,
move, move,

View File

@ -23,6 +23,9 @@ const authPlugins = [
require("./plugins/auth/local"), require("./plugins/auth/local"),
]; ];
// A random number that will force clients to reload the page if it differs
const serverHash = Math.floor(Date.now() * Math.random());
var manager = null; var manager = null;
module.exports = function() { module.exports = function() {
@ -135,7 +138,10 @@ module.exports = function() {
if (config.public) { if (config.public) {
performAuthentication.call(socket, {}); performAuthentication.call(socket, {});
} else { } else {
socket.emit("auth", {success: true}); socket.emit("auth", {
serverHash: serverHash,
success: true,
});
socket.on("auth", performAuthentication); socket.on("auth", performAuthentication);
} }
}); });