1

我是 Ruby、Sinatra 和 Pusher 的新手,所以这是一个基本问题。我正在尝试对 Pusher 客户端进行身份验证(使用 iOS 演示代码https://github.com/lukeredpath/libPusher)。当 iOS 客户端尝试加入在线状态频道时,下面的服务器代码失败并出现错误:

ArgumentError - wrong number of arguments (1 for 2):
    /Users/waveocean/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:665:in `render'
    web.rb:13:in `auth'
    web.rb:26:in `block in <main>'
    /Users/waveocean/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'

... snipped for brevity ...

这是代码:

require 'sinatra'
require 'pusher'
require 'thin'
Thin::HTTP_STATUS_CODES[403] = "FORBIDDEN"

Pusher.app_id = 'MY-APP-ID'
Pusher.key = 'MY-KEY'
Pusher.secret = 'MY-SECRET'

def auth
  response = Pusher[params[:channel_name]].authenticate(params[:socket_id], {:user_id => 101})
  render :json => response
end

 use Rack::Auth::Basic, "Protected Area" do |username, password|
   username == 'foo' && password == 'bar'
 end

 post '/presence/auth' do
   if params[:channel_name] == 'presence-demo'
      auth
   else
   #   render :text => "Forbidden", :status => '403'
   response.status = 403
   end
 end

有人可以提供建议或正确使用渲染吗?

4

1 回答 1

2

这是我发现的。render与 Rails 相关联,而不是严格意义上的 Ruby。要响应 Sinatra 路由,请在 auth 方法中使用以下内容:

def auth
  response = Pusher[params[:channel_name]].authenticate(params[:socket_id], {:user_id => 101})
  [200, {"Content-Type" => "application/json"}, response.to_json]
end

事实证明,Pusher iOS 项目演示提供了一个带有所需实现的 Scripts/auth_server.rb 文件。它与此处的安装说明一起记录:https ://github.com/lukeredpath/libPusher/wiki/Adding-libPusher-to-your-project 。

于 2012-12-19T17:37:11.157 回答