2012-12-19 44 views
1

我是Ruby,Sinatra和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 

有人能提供建議或渲染的正確使用?

回答

2

這是我發現的。 render與Rails關聯,而不是嚴格的Ruby。爲了給西納特拉路線迴應,請使用身份驗證方法如下:

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

事實證明,推動iOS的項目演示提供了所需的實施腳本/ auth_server.rb文件。這裏有安裝說明文檔:https://github.com/lukeredpath/libPusher/wiki/Adding-libPusher-to-your-project