Class: StripChart::WebSocket
- Inherits:
-
Object
- Object
- StripChart::WebSocket
- Defined in:
- lib/stripchart/websocket.rb
Instance Method Summary collapse
-
#initialize(channel) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #run! ⇒ Object
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 |