From 32b47921e5c48c2986b1d83f020504964765b878 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 28 Jun 2018 08:05:08 +0000 Subject: [PATCH] Better reconnect logic. Refetch messages over http once reconnected when exceeded 1 min --- .../titanembeds/blueprints/gateway/gateway.py | 2 ++ webapp/titanembeds/static/js/embed.js | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/webapp/titanembeds/blueprints/gateway/gateway.py b/webapp/titanembeds/blueprints/gateway/gateway.py index 815ce78..5ccb465 100644 --- a/webapp/titanembeds/blueprints/gateway/gateway.py +++ b/webapp/titanembeds/blueprints/gateway/gateway.py @@ -86,6 +86,8 @@ class Gateway(Namespace): emit("revoke") time.sleep(1000) disconnect() + else: + emit("ack") else: if not guild_accepts_visitors(guild_id): disconnect() diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 8a795f1..bc26356 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -37,6 +37,8 @@ var current_user_discord_id; // Current user discord snowflake id, eg mine is 140252024666062848 var visitor_mode = false; // Keep track of if using the visitor mode or authenticate mode var socket = null; // Socket.io object + var socket_last_ack = null; // Socket.io last acknowledgement Moment obj + var socket_error_should_refetch = false; // If true, the next ack will trigger a http refetch if socket connected var authenticated_users_list = []; // List of all authenticated users var unauthenticated_users_list = []; // List of all guest users var discord_users_list = []; // List of all discord users that are probably online @@ -1255,6 +1257,7 @@ } } fet.done(function(data) { + socket_error_should_refetch = false; var status = data.status; if (visitor_mode) { update_embed_userchip(false, null, "Titan", null, "0001", null); @@ -1929,6 +1932,13 @@ socket.on("guest_icon_change", function (icon) { global_guest_icon = icon.guest_icon; }); + + socket.on("ack", function () { + socket_last_ack = moment(); + if (socket && socket_error_should_refetch) { + run_fetch_routine(); + } + }); } function update_socket_channels() { @@ -1939,6 +1949,17 @@ } function send_socket_heartbeat() { + if (socket_last_ack) { + var now = moment(); + var duration = moment.duration(now.diff(socket_last_ack)).minutes(); + if (socket && duration >= 1) { // server must hanged, lets reconnect + socket_error_should_refetch = true; + socket.disconnect(); + socket = null; + initiate_websockets(); + } + } + if (socket) { socket.emit("heartbeat", {"guild_id": guild_id, "visitor_mode": visitor_mode}); }