Class: StripChart::WebSocket

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

Instance Method Summary collapse

Constructor Details

#initialize(channel) ⇒ WebSocket

Returns a new instance of WebSocket.



6
7
8
9
10
# File 'lib/stripchart/websocket.rb', line 6

def initialize(channel)
  @channel = channel
  @data = []
  @channel.subscribe { |msg| @data << msg }
end

Instance Method Details

#run!Object



12
13
14
15
16
17
18
19
20
# File 'lib/stripchart/websocket.rb', line 12

def run!
  EventMachine::WebSocket.start(:host => 'localhost', :port => 9998) do |ws|
    ws.onopen do
      @data.each { |msg| ws.send(JSON.generate(msg)) }
      sid = @channel.subscribe { |msg| ws.send(JSON.generate(msg)) }
      ws.onclose { @channel.unsubscribe(sid) }
    end
  end
end