Ruby on rails Rails action电缆专用耗电元件

Ruby on rails Rails action电缆专用耗电元件,ruby-on-rails,actioncable,Ruby On Rails,Actioncable,我正在努力使用动作电缆。在我的例子中,我有两个用户(通过designe)可以彼此共享任务 现在,当user#1与user#2共享任务(通过表单)时,所有经过身份验证的用户都会收到通知 我应该如何以及在何处识别我的用户#2,以便只向他广播 以下是我目前的代码: 连接.rb module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :current_user

我正在努力使用动作电缆。在我的例子中,我有两个用户(通过
designe
)可以彼此共享任务

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

我应该如何以及在何处识别我的
用户#2
,以便只向他广播

以下是我目前的代码:

连接.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
class TodoChannel < ApplicationCable::Channel
  def subscribed
    stream_from "todo_channel_#{current_user.id}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end


  def notify
    ActionCable.server.broadcast "todo_channel_#{current_user.id}", message: 'some message'(not implemented yet)
  end
end
class TodoChannel < ApplicationCable::Channel
  def subscribed
    stream_from "todo_channel_#{params['user_signed_cookie']}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end


  def notify(param_message)
    ActionCable.server.broadcast "todo_channel_#{param_message['user_signed_cookie']}", message: 'some message'(not implemented yet)
  end
end
todo_频道.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
class TodoChannel < ApplicationCable::Channel
  def subscribed
    stream_from "todo_channel_#{current_user.id}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end


  def notify
    ActionCable.server.broadcast "todo_channel_#{current_user.id}", message: 'some message'(not implemented yet)
  end
end
class TodoChannel < ApplicationCable::Channel
  def subscribed
    stream_from "todo_channel_#{params['user_signed_cookie']}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end


  def notify(param_message)
    ActionCable.server.broadcast "todo_channel_#{param_message['user_signed_cookie']}", message: 'some message'(not implemented yet)
  end
end

我以前也遇到过类似的情况,直到我意识到您实际上可以在频道中多次从调用
stream\u,并且用户将在同一频道连接中预订多个不同的“房间”。这意味着你基本上可以做到这一点

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

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def notify(data)
    # depending on data given from the user, send it to only one specific user or everyone listening to the "todo_channel_all"
    if data['message']['other_user_id']
      ActionCable.server.broadcast "todo_channel_#{data['message']['other_user_id']}", message: 'some message'
    else
      ActionCable.server.broadcast "todo_channel_all", message: 'some message'
    end

  end
end
class TodoChannel
该代码假设用户已经知道另一个用户的id并将其发送到频道,您可能需要用一些安全性或其他东西来包装该代码,我承认我对rails不是很有经验,因为我还在学习


将来可能对您有益的另一点是,您还可以在同一频道功能中广播多条消息/多次。这意味着您可能支持将任务发送给单个特定用户、特定用户列表或所有人。只需在用户列表/数组/任意项上迭代,并在他们个人的“todo#U通道{user.id}”上向每个用户广播任务/消息/通知/任意项

我最终采用了不同的方法。我会把它写在这里,以防有人觉得有用

通知具有必须通知的用户的id。因此,在模型中,我有:

after_commit :broadcast_notification, on: :create

def broadcast_notification
  ActionCable.server.broadcast "todo_channel_#{self.user_id}", message: 'some message'
end
当我将广播放入模型中时,我的
todo_channel.rb
如下所示:

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

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end
class TodoChannel
步骤1:让每个用户拥有唯一的会话令牌。订阅时,每个用户将在头中发送会话令牌,并且头在连接类中是可访问的。使用会话令牌查找用户

步骤2:在用户id上对用户进行流式处理

步骤#3:在共享任务时,也在请求中获取用户id,并在给定的用户id上广播。

这称为“私人聊天”。如果您真的想获得当前用户id,可以通过3种方式:

  • 一些AJAX
    onload
    todo.coffee
    调用服务器

  • 在Rails HTML视图中将
    current_user.id
    呈现为一个属性,然后通过jQuery在
    todo.coffee
    中获取它(如中所示)

  • 在用户登录时创建一个普通cookie,然后在
    todo.coffee

  • 但是您不应该使用
    current_user.id
    ,因为它不安全。如果您使用它,那么可能有人在同一个站点注册,只需提供一个随机的
    用户id
    ,就可以轻松收听其他用户的私人聊天

    而是使用已签名(例如Rails加密)cookie作为用户的唯一广播标识符。这样,如果你在同一个网站注册,你就永远不会知道其他用户的签名cookie,所以你就不能闯入外星人的私人聊天

    app/config/initializers/warden_hooks.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
    
    class TodoChannel < ApplicationCable::Channel
      def subscribed
        stream_from "todo_channel_#{current_user.id}"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
    
      def notify
        ActionCable.server.broadcast "todo_channel_#{current_user.id}", message: 'some message'(not implemented yet)
      end
    end
    
    class TodoChannel < ApplicationCable::Channel
      def subscribed
        stream_from "todo_channel_#{params['user_signed_cookie']}"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
    
      def notify(param_message)
        ActionCable.server.broadcast "todo_channel_#{param_message['user_signed_cookie']}", message: 'some message'(not implemented yet)
      end
    end
    

    todo_频道.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
    
    class TodoChannel < ApplicationCable::Channel
      def subscribed
        stream_from "todo_channel_#{current_user.id}"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
    
      def notify
        ActionCable.server.broadcast "todo_channel_#{current_user.id}", message: 'some message'(not implemented yet)
      end
    end
    
    class TodoChannel < ApplicationCable::Channel
      def subscribed
        stream_from "todo_channel_#{params['user_signed_cookie']}"
      end
    
      def unsubscribed
        # Any cleanup needed when channel is unsubscribed
      end
    
    
      def notify(param_message)
        ActionCable.server.broadcast "todo_channel_#{param_message['user_signed_cookie']}", message: 'some message'(not implemented yet)
      end
    end
    

    todo\u channel.rb
    代码仅在设置了
    app/channels/application\u电缆/连接.rb
    以提供
    当前用户时有效。这实际上是一个完整的例子,包括安全,