aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib
diff options
context:
space:
mode:
authorMatilda Smeds <matildasmeds@users.noreply.github.com>2018-12-09 18:28:00 +0100
committerGitHub <noreply@github.com>2018-12-09 18:28:00 +0100
commit317ad3a583fd3dbe06de8724c7fc1c6ded4cae8b (patch)
treec7084e546cbc2f289ac8cf2057c1010b4dc7f880 /actioncable/lib
parenta87c91cd3993d776dc987ac43b5f6c897baa3206 (diff)
parentbad1041b82df941d588ae2565f62424d88104933 (diff)
downloadrails-317ad3a583fd3dbe06de8724c7fc1c6ded4cae8b.tar.gz
rails-317ad3a583fd3dbe06de8724c7fc1c6ded4cae8b.tar.bz2
rails-317ad3a583fd3dbe06de8724c7fc1c6ded4cae8b.zip
Merge branch 'master' into guides_session_guidelines_2
Diffstat (limited to 'actioncable/lib')
-rw-r--r--actioncable/lib/action_cable.rb6
-rw-r--r--actioncable/lib/action_cable/connection/authorization.rb2
-rw-r--r--actioncable/lib/action_cable/connection/base.rb11
-rw-r--r--actioncable/lib/action_cable/server/base.rb4
-rw-r--r--actioncable/lib/rails/generators/channel/templates/javascript/index.js.tt4
5 files changed, 20 insertions, 7 deletions
diff --git a/actioncable/lib/action_cable.rb b/actioncable/lib/action_cable.rb
index d261d4112e..cb9dfa2268 100644
--- a/actioncable/lib/action_cable.rb
+++ b/actioncable/lib/action_cable.rb
@@ -33,10 +33,16 @@ module ActionCable
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"].freeze
}
diff --git a/actioncable/lib/action_cable/connection/authorization.rb b/actioncable/lib/action_cable/connection/authorization.rb
index a22179d988..aef3386f71 100644
--- a/actioncable/lib/action_cable/connection/authorization.rb
+++ b/actioncable/lib/action_cable/connection/authorization.rb
@@ -5,7 +5,7 @@ module ActionCable
module Authorization
class UnauthorizedError < StandardError; end
- # Closes the \WebSocket connection if it is open and returns a 404 "File not Found" response.
+ # Closes the WebSocket connection if it is open and returns a 404 "File not Found" response.
def reject_unauthorized_connection
logger.error "An unauthorized connection attempt was rejected"
raise UnauthorizedError
diff --git a/actioncable/lib/action_cable/connection/base.rb b/actioncable/lib/action_cable/connection/base.rb
index 11a1f1a5e8..0044afad98 100644
--- a/actioncable/lib/action_cable/connection/base.rb
+++ b/actioncable/lib/action_cable/connection/base.rb
@@ -95,7 +95,12 @@ module ActionCable
end
# Close the WebSocket connection.
- def close
+ def close(reason: nil, reconnect: true)
+ transmit(
+ type: ActionCable::INTERNAL[:message_types][:disconnect],
+ reason: reason,
+ reconnect: reconnect
+ )
websocket.close
end
@@ -170,7 +175,7 @@ module ActionCable
message_buffer.process!
server.add_connection(self)
rescue ActionCable::Connection::Authorization::UnauthorizedError
- respond_to_invalid_request
+ close(reason: ActionCable::INTERNAL[:disconnect_reasons][:unauthorized], reconnect: false) if websocket.alive?
end
def handle_close
@@ -211,7 +216,7 @@ module ActionCable
end
def respond_to_invalid_request
- close if websocket.alive?
+ close(reason: ActionCable::INTERNAL[:disconnect_reasons][:invalid_request]) if websocket.alive?
logger.error invalid_request_message
logger.info finished_request_message
diff --git a/actioncable/lib/action_cable/server/base.rb b/actioncable/lib/action_cable/server/base.rb
index 1ee03f6dfc..2b9e1cba3b 100644
--- a/actioncable/lib/action_cable/server/base.rb
+++ b/actioncable/lib/action_cable/server/base.rb
@@ -36,7 +36,9 @@ module ActionCable
end
def restart
- connections.each(&:close)
+ connections.each do |connection|
+ connection.close(reason: ActionCable::INTERNAL[:disconnect_reasons][:server_restart])
+ end
@mutex.synchronize do
# Shutdown the worker pool
diff --git a/actioncable/lib/rails/generators/channel/templates/javascript/index.js.tt b/actioncable/lib/rails/generators/channel/templates/javascript/index.js.tt
index 5da1ce2dce..0cfcf74919 100644
--- a/actioncable/lib/rails/generators/channel/templates/javascript/index.js.tt
+++ b/actioncable/lib/rails/generators/channel/templates/javascript/index.js.tt
@@ -1,5 +1,5 @@
-// Load all the channels within this directory and all subdirectories.
+// Load all the channels within this directory and all subdirectories.
// Channel files must be named *_channel.js.
-const channels = require.context('.', true, /\_channel\.js$/)
+const channels = require.context('.', true, /_channel\.js$/)
channels.keys().forEach(channels)