Rails:Faye 为我工作,但由于某种未知原因仍然在控制台中给出一些 JS 错误,我应该担心吗?



我正在使用Private_Pub建立在Faye上的gem,我正在使用另一个由这个博客提供的Faye应用程序,我发现的项目。 让 Faye 在一个应用程序上运行,而我的实际网站在另一个应用程序上运行。

火狐控制台中的错误:

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

任何建议: ?

我还在application_controller中添加了一个after_filter以允许域请求

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

为了解决这个问题,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中包含相同的内容,但将域名更改为原始/实际/主轨道应用程序。

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

最新更新