Class: PingPongPear::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/ping_pong_pear.rb

Instance Method Summary collapse

Constructor Details

#initializeListener

Returns a new instance of Listener.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ping_pong_pear.rb', line 53

def initialize
  @multicast_addr = MULTICAST_ADDR
  @bind_addr      = "0.0.0.0"
  @port           = UDP_PORT

  @socket = UDPSocket.new
  membership = IPAddr.new(@multicast_addr).hton + IPAddr.new(@bind_addr).hton

  @socket.setsockopt :IPPROTO_IP, :IP_ADD_MEMBERSHIP, membership
  @socket.setsockopt :SOL_SOCKET, :SO_REUSEPORT, 1

  @socket.bind @bind_addr, @port
end

Instance Method Details

#startObject



67
68
69
70
71
72
# File 'lib/ping_pong_pear.rb', line 67

def start
  loop do
    message, _ = @socket.recvfrom 1024
    yield JSON.parse(message)
  end
end