小编Ill*_*zma的帖子

Rails动作电缆特定消费者

我正在努力使用动作电缆.在我的情况下,我有几个用户(via Devise)可以互相分享任务.

现在,当所有经过身份验证的用户user#1共享任务(通过表单)user#2接收通知时.

我应该如何以及在何处识别我user#2的广播?

到目前为止,这是我的代码:

connection.rb

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.id
    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
Run Code Online (Sandbox Code Playgroud)

cable.js

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);
Run Code Online (Sandbox Code Playgroud)

todo_channel.rb

class TodoChannel < ApplicationCable::Channel
  def subscribed
    stream_from "todo_channel_#{current_user.id}"
  end

  def unsubscribed …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails actioncable

3
推荐指数
1
解决办法
3364
查看次数

标签 统计

actioncable ×1

ruby-on-rails ×1