Object
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 19 def initialize(host, port) @server = TCPServer.new(host, port) @sockets = [] end
Broadcasts given payload to all connected sockets
# File lib/listen/tcp/broadcaster.rb, line 39 def broadcast(payload) @sockets.each do |socket| unicast(socket, payload) end end
Cleans up sockets and server
# File lib/listen/tcp/broadcaster.rb, line 30 def finalize if @server @sockets.clear @server.close @server = nil end end
Handles incoming socket connection
# File lib/listen/tcp/broadcaster.rb, line 65 def handle_connection(socket) @sockets << socket end
Continuously accept and handle incoming connections
# File lib/listen/tcp/broadcaster.rb, line 58 def run while socket = @server.accept handle_connection(socket) end end
Asynchronously start accepting connections
# File lib/listen/tcp/broadcaster.rb, line 25 def start async.run end
Unicasts payload to given socket
@return [Boolean] whether writing to socket was succesful
# File lib/listen/tcp/broadcaster.rb, line 49 def unicast(socket, payload) socket.write(payload) true rescue IOError, Errno::ECONNRESET, Errno::EPIPE @sockets.delete(socket) false end
Generated with the Darkfish Rdoc Generator 2.