Rails 网络套接字 - 授权

Rails web socket - 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={}>

怎么了? 非常感谢!

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