Class: Firehose::Rack::Consumer::WebSocket::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/firehose/rack/consumer/web_socket.rb

Direct Known Subclasses

DefaultHandler, MultiplexingHandler

Instance Method Summary collapse

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.onmessage = 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.message.inspect}`"
end

#parse_message(event) ⇒ Object



36
37
38
# File 'lib/firehose/rack/consumer/web_socket.rb', line 36

def parse_message(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 send_message(message)
  @ws.send JSON.generate(message)
end