1
0
Fork 0
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:
David Heinemeier Hansson 2015-06-28 20:24:50 +02:00
parent f61467ec5b
commit e1a99a83ca
4 changed files with 31 additions and 25 deletions

View file

@ -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

View file

@ -1,6 +1,7 @@
module ActionCable
module Server
autoload :Base, 'action_cable/server/base'
autoload :Broadcasting, 'action_cable/server/broadcasting'
autoload :Worker, 'action_cable/server/worker'
end
end

View file

@ -1,6 +1,8 @@
module ActionCable
module Server
class Base
include ActionCable::Server::Broadcasting
cattr_accessor(:logger, instance_reader: true) { Rails.logger }
attr_accessor :registered_channels, :redis_config, :log_tags
@ -49,14 +51,6 @@ module ActionCable
@remote_connections ||= RemoteConnections.new(self)
end
def broadcaster_for(channel)
Broadcaster.new(self, channel)
end
def broadcast(channel, message)
broadcaster_for(channel).broadcast(message)
end
def connection_identifiers
@connection_class.identifiers
end

View 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