2

I am using Faye websocket implementation. When a websocket is detected I want to make sure the user sent a given header before I open the connection, or else I want to return an error. I tried returning error 401 using [] but I keep oberving that the code continues execution and the websocket is created anyway. I managed to prevent it by adding return after it, but I am not sure this is the right way. On the client side I am implementing it in python using ws4py and when I return I get an exception raised indicating it received a 302 code, not the 401 I was expecting to send.

My ruby code (without the websocket events code) follows:

  App = lambda do |env|
    if Faye::WebSocket.websocket?(env)
      unless env['HTTP_MY_HEADER']
        [401, {'Content-Type' => 'application/json'}, '{"message": "I was expecting a header here"}']
        #return (??)
      end

      ws = Faye::WebSocket.new(env)
      # Rest of websocket call handling
    else
      # Normal HTTP request
      [200, {'Content-Type' => 'text/plain'}, ['Hello']]
    end
  end
XAnguera
  • 1,157
  • 1
  • 11
  • 25
  • This doesn't look Rails related...? seems a pure Rack application in Ruby. Consider removing the Rails tag, perhaps adding the Rack tag. Also consider using a `module` and defining a method `call` instead of using a `lambda`, since you can return from a function and it's easier to manage the flow of the logic. – Myst Feb 12 '17 at 07:49
  • Possible duplicate of [return json from ruby using rack](https://stackoverflow.com/questions/34269561/return-json-from-ruby-using-rack) – Paul Sweatte May 31 '17 at 05:05

0 Answers0