Class: Radiowaves::Server

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/radiowaves/server.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



8
9
10
11
# File 'lib/radiowaves/server.rb', line 8

def initialize(host, port)
  @host = host
  @port = port
end

Instance Method Details

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/radiowaves/server.rb', line 13

def start
  EventMachine.run {
    @channel = EM::Channel.new
    EventMachine::WebSocket.start(:host => @host, :port => @port) do |ws|
      ws.onopen {
        logger.debug "*** ws.onopen"
        sid = @channel.subscribe { |msg| 
          ws.send msg 
        }
        ws.onmessage { |msg|
          @channel.push "#{msg}"
        }
        ws.onclose {
          logger.debug "*** ws.onclose: #{sid}"
          @channel.unsubscribe(sid)
        }
      }
    end
  }
end