class StripChart::WebSocket

Public Class Methods

new(channel) click to toggle source
# File lib/stripchart/websocket.rb, line 6
def initialize(channel)
  @channel = channel
  @data = []
  @channel.subscribe { |msg| @data << msg }
end

Public Instance Methods

run!() click to toggle source
# 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