如何从Rails机架通话返回?

时间:2016-12-21 15:49:15

标签: ruby websocket rack ws4py

我正在使用Faye websocket implementation。当检测到websocket时,我想确保用户在打开连接之前发送了一个给定的头,否则我想返回一个错误。 我尝试使用[]返回错误401但我仍然认为代码继续执行并且无论如何都会创建websocket。我设法在它之后添加return来阻止它,但我不确定这是正确的方法。在客户端我使用ws4py在python中实现它,当我返回时,我得到一个异常,表明它收到了302代码,而不是我期望发送的401。

我的ruby代码(没有websocket事件代码)如下:

  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

0 个答案:

没有答案