Class: WebsocketPipe
- Inherits:
-
Object
- Object
- WebsocketPipe
- Defined in:
- lib/websocket-pipe.rb
Constant Summary collapse
- POLL_INTERVAL =
0.1
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(reader, host_info = {}) ⇒ WebsocketPipe
constructor
A new instance of WebsocketPipe.
- #start! ⇒ Object
Constructor Details
#initialize(reader, host_info = {}) ⇒ WebsocketPipe
Returns a new instance of WebsocketPipe.
7 8 9 10 11 12 |
# File 'lib/websocket-pipe.rb', line 7 def initialize(reader, host_info={}) @clients = [] @reader = reader @host_info = {:host => "0.0.0.0", :port => 8080} .merge(host_info) end |
Class Method Details
.fork! ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/websocket-pipe.rb', line 38 def self.fork! ws_reader, ws_writer = IO.pipe ws_pid = fork do ws_writer.close WebsocketPipe.new(ws_reader).start! end ws_reader.close [ws_pid, ws_writer] end |
Instance Method Details
#start! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/websocket-pipe.rb', line 14 def start! last_msg = nil EM.run do EM::WebSocket.run(@host_info) do |ws| ws.onopen do @clients << ws ws.send(last_msg) end ws.onclose do @clients.delete(ws) end end EM::PeriodicTimer.new(POLL_INTERVAL) do if @reader.ready? msg = @reader.gets @clients.each {|client| client.send(msg)} last_msg = msg end end end end |