Rails Web套接字 - 授权

时间:2015-09-28 15:40:45

标签: ruby-on-rails ruby sockets ember.js authorization

我有Ember.js客户端和RoR服务器端有gem“websocket-rails”。

我需要获得私有频道,授权仅为一个用户发送消息。

这是我尝试的方式:

class AuthorizationController < WebsocketRails::BaseController

    before_action :require_login

    def authorize_channels
        # The channel name will be passed inside the message Hash
        puts '*'*400
        ap current_user//Here I see the user

        channel = WebsocketRails[message[:channel]]
        accept_channel current_user

    end


    def require_login

        sign_in(User.find(4))

    end
end

我的events.rb是:

WebsocketRails::EventMap.describe do
    namespace :websocket_rails do
        subscribe :subscribe, :to => AuthorizationController, :with_method => :authorize_channels
    end

end

但是当我尝试发送消息时:

WebsocketRails.users[4].trigger(:test_event, { message: 'This is a private message from WebSocket Rails' } )

我收到错误#<NoMethodError: undefined method ``trigger' for #<WebsocketRails::UserManager::MissingConnection:0x007ff5bb50dc88>>

如果我尝试打印WebsocketRails.users,我会看到:

#<WebsocketRails::UserManager:0x007ff5bb617d68 @users={}>

有什么问题? 很多!

1 个答案:

答案 0 :(得分:0)

如果您使用rails-api gem,它会禁用会话,这就是问题所在。