Rails 5 ActionCable错误描述和拒绝连接

时间:2017-01-17 13:48:15

标签: ruby-on-rails devise ruby-on-rails-5 actioncable

我与使用devise进行身份验证的ActionCable进行了基本聊天。

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.email
    end

    protected

    def find_verified_user # this checks whether a user is authenticated with devise
      if verified_user = env['warden'].user
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

但是当用户打开聊天并且拒绝连接时(因为用户已经注销),我需要显示登录屏幕。

问题是在前端我无法找到断开连接的原因。

如何使用params发送拒绝,例如“未经授权”?

1 个答案:

答案 0 :(得分:0)

def find_verified_user # this checks whether a user is authenticated with devise
  if verified_user = env['warden'].user
    verified_user
  else
    message = "The user is not found. Connection rejected."

    logger.add_tags 'ActionCable', message # to console

    self.transmit error: message # this is what you wanted

    reject_unauthorized_connection
  end
end

另请参阅:How to terminate subscription to an actioncable channel from server?