Initializes a Celluloid::IO-powered TCP-broadcaster
@param [String] host to broadcast on @param [String] port to broadcast on
Note: Listens on all addresses when host is nil
# File lib/listen/tcp/broadcaster.rb, line 17 def initialize(host, port) @sockets = [] @server = TCPServer.new(host, port) rescue _log :error, "Broadcaster.initialize: #{$!.inspect}:#{$@.join("\n")}" raise end
Broadcasts given payload to all connected sockets
# File lib/listen/tcp/broadcaster.rb, line 41 def broadcast(payload) active_sockets = @sockets.select do |socket| _unicast(socket, payload) end @sockets.replace(active_sockets) end
Cleans up sockets and server
# File lib/listen/tcp/broadcaster.rb, line 31 def finalize @sockets.map(&:close) if @sockets @sockets = nil return unless @server @server.close @server = nil end
Continuously accept and handle incoming connections
# File lib/listen/tcp/broadcaster.rb, line 49 def run while socket = @server.accept @sockets << socket end rescue Celluloid::Task::TerminatedError _log :debug, "TCP adapter was terminated: #{$!.inspect}" rescue _log :error, "Broadcaster.run: #{$!.inspect}:#{$@.join("\n")}" raise end
Generated with the Darkfish Rdoc Generator 2.