Class: Firehose::Rack::Consumer::WebSocket::Handler
- Inherits:
-
Object
- Object
- Firehose::Rack::Consumer::WebSocket::Handler
- Defined in:
- lib/firehose/rack/consumer/web_socket.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#error(event) ⇒ Object
Log errors if a socket fails.
-
#initialize(ws) ⇒ Handler
constructor
A new instance of Handler.
- #parse_message(event) ⇒ Object
-
#send_message(message) ⇒ Object
Send a JSON message to the client Expects message to be a Hash.
Constructor Details
#initialize(ws) ⇒ Handler
Returns a new instance of Handler.
26 27 28 29 30 31 32 33 34 |
# File 'lib/firehose/rack/consumer/web_socket.rb', line 26 def initialize(ws) @ws = ws @req = ::Rack::Request.new ws.env # Setup the event handlers from this class. @ws.onopen = method :open @ws.onclose = method :close @ws.onerror = method :error @ws. = method :message end |
Instance Method Details
#error(event) ⇒ Object
Log errors if a socket fails. ‘close` will fire after this to clean up any remaining connectons.
48 49 50 |
# File 'lib/firehose/rack/consumer/web_socket.rb', line 48 def error(event) Firehose.logger.error "WS connection `#{@req.path}` error. Message: `#{event..inspect}`" end |
#parse_message(event) ⇒ Object
36 37 38 |
# File 'lib/firehose/rack/consumer/web_socket.rb', line 36 def (event) JSON.parse(event.data, :symbolize_names => true) rescue {} end |
#send_message(message) ⇒ Object
Send a JSON message to the client Expects message to be a Hash
42 43 44 |
# File 'lib/firehose/rack/consumer/web_socket.rb', line 42 def () @ws.send JSON.generate() end |