加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 5 ActionCable错误描述和拒绝连接

发布时间:2020-12-17 01:57:12 所属栏目:百科 来源:网络整理
导读:我与使用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',curr
我与使用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

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

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

如何发送带有参数的拒绝,例如“未经授权”?

解决方法

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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读