class Firehose::Rack::Consumer::WebSocket::Handler

Public Class Methods

new(ws) click to toggle source
# 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

Public Instance Methods

error(event) click to toggle source

Log errors if a socket fails. ‘close` will fire after this to clean up any remaining connectons.

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

Send a JSON message to the client Expects message to be a Hash

# File lib/firehose/rack/consumer/web_socket.rb, line 42
def send_message(message)
  @ws.send JSON.generate(message)
end