【问题标题】:Rails 5 ActionCable error description and rejected connectionRails 5 ActionCable 错误描述和拒绝连接
【发布时间】:2017-06-01 14:46:55
【问题描述】:

我与通过 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

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

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

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

【问题讨论】:

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


    【解决方案1】:
    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?

    【讨论】:

      猜你喜欢
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多