mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make broadcasting a concern
This commit is contained in:
parent
f61467ec5b
commit
e1a99a83ca
4 changed files with 31 additions and 25 deletions
|
@ -1,17 +0,0 @@
|
||||||
module ActionCable
|
|
||||||
class Broadcaster
|
|
||||||
attr_reader :server, :channel, :redis
|
|
||||||
delegate :logger, to: :server
|
|
||||||
|
|
||||||
def initialize(server, channel)
|
|
||||||
@server = server
|
|
||||||
@channel = channel
|
|
||||||
@redis = @server.threaded_redis
|
|
||||||
end
|
|
||||||
|
|
||||||
def broadcast(message)
|
|
||||||
logger.info "[ActionCable] Broadcasting to #{channel}: #{message}"
|
|
||||||
redis.publish channel, message.to_json
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,6 +1,7 @@
|
||||||
module ActionCable
|
module ActionCable
|
||||||
module Server
|
module Server
|
||||||
autoload :Base, 'action_cable/server/base'
|
autoload :Base, 'action_cable/server/base'
|
||||||
|
autoload :Broadcasting, 'action_cable/server/broadcasting'
|
||||||
autoload :Worker, 'action_cable/server/worker'
|
autoload :Worker, 'action_cable/server/worker'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module ActionCable
|
module ActionCable
|
||||||
module Server
|
module Server
|
||||||
class Base
|
class Base
|
||||||
|
include ActionCable::Server::Broadcasting
|
||||||
|
|
||||||
cattr_accessor(:logger, instance_reader: true) { Rails.logger }
|
cattr_accessor(:logger, instance_reader: true) { Rails.logger }
|
||||||
|
|
||||||
attr_accessor :registered_channels, :redis_config, :log_tags
|
attr_accessor :registered_channels, :redis_config, :log_tags
|
||||||
|
@ -49,14 +51,6 @@ module ActionCable
|
||||||
@remote_connections ||= RemoteConnections.new(self)
|
@remote_connections ||= RemoteConnections.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def broadcaster_for(channel)
|
|
||||||
Broadcaster.new(self, channel)
|
|
||||||
end
|
|
||||||
|
|
||||||
def broadcast(channel, message)
|
|
||||||
broadcaster_for(channel).broadcast(message)
|
|
||||||
end
|
|
||||||
|
|
||||||
def connection_identifiers
|
def connection_identifiers
|
||||||
@connection_class.identifiers
|
@connection_class.identifiers
|
||||||
end
|
end
|
||||||
|
|
28
lib/action_cable/server/broadcasting.rb
Normal file
28
lib/action_cable/server/broadcasting.rb
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
module ActionCable
|
||||||
|
module Server
|
||||||
|
module Broadcasting
|
||||||
|
def broadcaster_for(channel)
|
||||||
|
Broadcaster.new(self, channel)
|
||||||
|
end
|
||||||
|
|
||||||
|
def broadcast(channel, message)
|
||||||
|
broadcaster_for(channel).broadcast(message)
|
||||||
|
end
|
||||||
|
|
||||||
|
class Broadcaster
|
||||||
|
attr_reader :server, :channel, :redis
|
||||||
|
delegate :logger, to: :server
|
||||||
|
|
||||||
|
def initialize(server, channel)
|
||||||
|
@server, @channel = server, channel
|
||||||
|
@redis = @server.threaded_redis
|
||||||
|
end
|
||||||
|
|
||||||
|
def broadcast(message)
|
||||||
|
logger.info "[ActionCable] Broadcasting to #{channel}: #{message}"
|
||||||
|
redis.publish channel, message.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue