2

我正在使用基于Faye构建Private_Pub gem ,并且我正在使用我发现的这个博客提供的另一个用于 Faye 的应用程序。让 Faye 在一个应用程序上运行,而我的实际站点在另一个应用程序上运行。

FireFox 控制台中的错误:

The connection to ws://xxxxxxxxxxx.herokuapp.com/faye was interrupted while the page was loading. Faye.js

在 Chrome 中:

WebSocket connection to 'ws://xxxxxxxxxxx.herokuapp.com/faye' failed: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0 

在聊天(Faye)应用程序日志中,我在日志中得到了这个:

at=error code=H12 desc="Request timeout" method=GET path=/faye host=xxxxxxxxxxx.herokuapp.com request_id=xxxxxxxxxxx fwd="xxxxxxxxxxx" dyno=web.1 connect=31ms service=30112ms status=503 bytes=0

有什么建议么: ?

我也添加了一个after_filterapplication_controller允许域请求

4

2 回答 2

2

这也是我的应用程序的一个问题,heroku 的超时为 30,如果 faye.ru/config.ru 的超时设置超过 30,则会出现此错误。尝试将其减少到 25 或其他东西。 faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

于 2014-02-15T12:00:26.550 回答
1

为了解决这个问题,Rails Apps需要让对方访问或者说互相ping。为了做到这一点,我们必须使用CORS之类的东西来允许来自其他域的 AJAX 请求。这是我用来允许方法访问文章的一种更简单的方法,起初听起来可能令人困惑。

要在双方都执行此操作,您还需要为他们创建一个实际完整的应用程序,这Faye与我在问题中附加的 Faye 聊天服务器 Repo 上给出的内容不同。

  • 步骤1 :

在实际/主应用程序中,将其包含在您的application_controller.rb

  before_filter :cors_preflight_check
  after_filter :cors_set_access_control_headers
  #For all responses in this controller, return the CORS access control headers.
  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = 'http://YOURFAYEAPP.com'
    headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
    headers['Access-Control-Request-Method'] = 'http://YOURFAYEAPP.com'
    headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  end
  # If this is a preflight OPTIONS request, then short-circuit the
  # request, return only the necessary headers and return an empty
  # text/plain.
  def cors_preflight_check
    if request.method == :options
      headers['Access-Control-Allow-Origin'] = 'http://YOURFAYEAPP.com'
      headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
      headers['Access-Control-Request-Method'] = 'http://YOURFAYEAPP.com'
      headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
      render :text => '', :content_type => 'text/plain'
    end
end
  • 第2步:

    在 Faye 应用端包含相同的东西application_controller.rb,但将域名更改为 Original/Actual/Main Rails 应用程序。

这样,两者都可以发送和接收请求。不要*代替 request parameter,这是一个安全问题。你可以减少methods到只有GetPost

于 2014-03-30T01:57:20.640 回答