提交 d81a9d79 编写于 作者: M Matthew Draper

Merge pull request #23488 from maclover7/actioncable-defaults

Enable ActionCable routes by default
......@@ -9,7 +9,7 @@
getConfig: (name) ->
element = document.head.querySelector("meta[name='action-cable-#{name}']")
element?.getAttribute("content")
element?.getAttribute("content") ? '/cable'
createWebSocketURL: (url) ->
if url and not /^wss?:/i.test(url)
......
......@@ -6,9 +6,11 @@ class ActionCable.Connection
@reopenDelay: 500
constructor: (@consumer) ->
@open()
send: (data) ->
unless @isOpen()
@open()
if @isOpen()
@webSocket.send(JSON.stringify(data))
true
......
......@@ -6,7 +6,7 @@
module ActionCable
class Railtie < Rails::Engine # :nodoc:
config.action_cable = ActiveSupport::OrderedOptions.new
config.action_cable.url = '/cable'
config.action_cable.mount_path = '/cable'
config.eager_load_namespaces << ActionCable
......@@ -40,5 +40,16 @@ class Railtie < Rails::Engine # :nodoc:
options.each { |k,v| send("#{k}=", v) }
end
end
initializer "action_cable.routes" do
config.after_initialize do |app|
config = app.config
unless config.action_cable.mount_path.nil?
app.routes.prepend do
mount ActionCable.server => config.action_cable.mount_path, internal: true
end
end
end
end
end
end
......@@ -20,9 +20,20 @@ module ActionCableHelper
# Make sure to specify the correct server location in each of your environments
# config file:
#
# config.action_cable.url = "ws://example.com:28080"
# config.action_cable.mount_path = "/cable123"
# <%= action_cable_meta_tag %> would render:
# => <meta name="action-cable-url" content="/cable123" />
#
# config.action_cable.url = "ws://actioncable.com"
# <%= action_cable_meta_tag %> would render:
# => <meta name="action-cable-url" content="ws://actioncable.com" />
#
def action_cable_meta_tag
tag "meta", name: "action-cable-url", content: Rails.application.config.action_cable.url
tag "meta", name: "action-cable-url", content: (
ActionCable.server.config.url ||
ActionCable.server.config.mount_path ||
raise("No Action Cable URL configured -- please configure this at config.action_cable.url")
)
end
end
end
......
......@@ -6,7 +6,7 @@ class Configuration
attr_accessor :logger, :log_tags
attr_accessor :connection_class, :worker_pool_size
attr_accessor :disable_request_forgery_protection, :allowed_request_origins
attr_accessor :cable, :url
attr_accessor :cable, :url, :mount_path
attr_accessor :channel_paths # :nodoc:
......
......@@ -613,6 +613,17 @@ There are a few configuration options available in Active Support:
* `config.active_job.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Active Job. You can retrieve this logger by calling `logger` on either an Active Job class or an Active Job instance. Set to `nil` to disable logging.
### Configuring Action Cable
* `config.action_cable.url` accepts a string for the URL for where
you are hosting your Action Cable server. You would use this option
if you are running Action Cable servers that are separated from your
main application.
* `config.action_cable.mount_path` accepts a string for where to mount Action
Cable, as apart of the main server process. Defaults to `/cable`.
You can set this as nil to not mount Action Cable as apart of your
normal Rails server.
### Configuring a Database
Just about every Rails application will interact with a database. You can connect to the database by setting an environment variable `ENV['DATABASE_URL']` or by using a configuration file called `config/database.yml`.
......
# Action Cable provides the framework to deal with WebSockets in Rails.
# You can generate new channels where WebSocket features live using the rails generate channel command.
#
# Turn on the cable connection by removing the comments after the require statements (and ensure it's also on in config/routes.rb).
#
#= require action_cable
#= require_self
#= require_tree ./channels
#
# @App ||= {}
# App.cable = ActionCable.createConsumer()
@App ||= {}
App.cable = ActionCable.createConsumer()
......@@ -3,9 +3,6 @@
<head>
<title><%= camelized %></title>
<%%= csrf_meta_tags %>
<%- unless options[:skip_action_cable] -%>
<%%= action_cable_meta_tag %>
<%- end -%>
<%- if options[:skip_javascript] -%>
<%%= stylesheet_link_tag 'application', media: 'all' %>
......
......@@ -91,5 +91,9 @@ Rails.application.configure do
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
# Don't mount Action Cable in the main server process.
# config.action_cable.mount_path = nil
# config.action_cable.url = "ws://example.com"
<%- end -%>
end
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
# Serve websocket cable requests in-process
# mount ActionCable.server => '/cable'
end
......@@ -118,7 +118,7 @@ def test_should_not_eager_load_model_for_rake
end
def test_code_statistics_sanity
assert_match "Code LOC: 14 Test LOC: 0 Code to Test Ratio: 1:0.0",
assert_match "Code LOC: 16 Test LOC: 0 Code to Test Ratio: 1:0.0",
Dir.chdir(app_path){ `bin/rails stats` }
end
......
......@@ -406,9 +406,6 @@ def test_generator_if_skip_action_cable_is_given
assert_no_file "config/cable.yml"
assert_no_file "app/assets/javascripts/cable.coffee"
assert_no_file "app/channels"
assert_file "app/views/layouts/application.html.erb" do |content|
assert_no_match(/action_cable_meta_tag/, content)
end
assert_file "Gemfile" do |content|
assert_no_match(/redis/, content)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册