From 58dbc1c2ed0e372d9cae4c9e3baebb679a726dc3 Mon Sep 17 00:00:00 2001
From: Mick Staugaard <mick@staugaard.com>
Date: Thu, 11 Oct 2018 13:47:16 -0700
Subject: Stop trying to reconnect on unauthorized cable connections

---
 actioncable/app/assets/javascripts/action_cable.js    | 14 +++++++++++++-
 actioncable/app/javascript/action_cable/connection.js |  5 ++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

(limited to 'actioncable/app')

diff --git a/actioncable/app/assets/javascripts/action_cable.js b/actioncable/app/assets/javascripts/action_cable.js
index ee11c62eb6..65e32d6c3f 100644
--- a/actioncable/app/assets/javascripts/action_cable.js
+++ b/actioncable/app/assets/javascripts/action_cable.js
@@ -136,10 +136,16 @@
   var INTERNAL = {
     message_types: {
       welcome: "welcome",
+      disconnect: "disconnect",
       ping: "ping",
       confirmation: "confirm_subscription",
       rejection: "reject_subscription"
     },
+    disconnect_reasons: {
+      unauthorized: "unauthorized",
+      invalid_request: "invalid_request",
+      server_restart: "server_restart"
+    },
     default_mount_path: "/cable",
     protocols: [ "actioncable-v1-json", "actioncable-unsupported" ]
   };
@@ -251,12 +257,18 @@
       if (!this.isProtocolSupported()) {
         return;
       }
-      var _JSON$parse = JSON.parse(event.data), identifier = _JSON$parse.identifier, message = _JSON$parse.message, type = _JSON$parse.type;
+      var _JSON$parse = JSON.parse(event.data), identifier = _JSON$parse.identifier, message = _JSON$parse.message, reason = _JSON$parse.reason, reconnect = _JSON$parse.reconnect, type = _JSON$parse.type;
       switch (type) {
        case message_types.welcome:
         this.monitor.recordConnect();
         return this.subscriptions.reload();
 
+       case message_types.disconnect:
+        logger.log("Disconnecting. Reason: " + reason);
+        return this.close({
+          allowReconnect: reconnect
+        });
+
        case message_types.ping:
         return this.monitor.recordPing();
 
diff --git a/actioncable/app/javascript/action_cable/connection.js b/actioncable/app/javascript/action_cable/connection.js
index 8aa4fe1266..b2910cb2a6 100644
--- a/actioncable/app/javascript/action_cable/connection.js
+++ b/actioncable/app/javascript/action_cable/connection.js
@@ -117,11 +117,14 @@ Connection.reopenDelay = 500
 Connection.prototype.events = {
   message(event) {
     if (!this.isProtocolSupported()) { return }
-    const {identifier, message, type} = JSON.parse(event.data)
+    const {identifier, message, reason, reconnect, type} = JSON.parse(event.data)
     switch (type) {
       case message_types.welcome:
         this.monitor.recordConnect()
         return this.subscriptions.reload()
+      case message_types.disconnect:
+        logger.log(`Disconnecting. Reason: ${reason}`)
+        return this.close({allowReconnect: reconnect})
       case message_types.ping:
         return this.monitor.recordPing()
       case message_types.confirmation:
-- 
cgit v1.2.3