操作电缆:在通道中调用自定义方法

时间:2018-04-18 11:19:46

标签: ruby-on-rails reactjs actioncable

我试图实施动作电缆。这很好用,但是当我连接到频道时,我想要接收一些数据(这里我想知道有多少用户已经连接)。

我在我的频道上创建了一个自定义方法nbUsers:

class OnTheSamePageChannel < ApplicationCable::Channel

  @count = 0

  def subscribed
    ...
    @count++
    stream_for(params[:url])
  end

  def unsubscribed
    ...
    @count--
  end

  def nbUsers
    pp 'debug: nbUsersCalled'
    @count
  end

我的javascript:

export default class OnSamePageSubscription extends React.Component {

    constructor(props) {
        super(props);

        this.cable = ActionCable.createConsumer(WEB_SOCKET_HOST);
        this.subscribe = () => {
            this.channel = this.cable.subscriptions.create(
                {
                    channel: 'OnTheSamePageChannel',
                    url: props.url,
                    current_user: props.current_user
                },
                {
                    connected: this.connected,
                    disconnected: this.disconnected,
                    received: this.received,
                    rejected: this.rejected
                }
            );
        };
        this.unsubscribe = () => {
            this.channel.unsubscribe()
        };
    }

    received = (data) => {
        console.log(`Connected user : ${data.user}`);

        this.props.onUpdate(data);
    };

    connected = () => {
        console.log(`Tracking connection`);
    };

    disconnected = () => {
        console.warn(`Tracking disconnected.`);
    };

    rejected = () => {
        console.warn('Tracking rejected');
    };

}

问题是:如何调用此方法(nbUsers)并获得结果?

我试过https://robots.thoughtbot.com/talking-to-actioncable-without-rails 但这不起作用

2 个答案:

答案 0 :(得分:1)

您可以使用perform调用它。这是代码 this.perform( “nbUsers”)

答案 1 :(得分:1)

class OnTheSamePageChannel < ApplicationCable::Channel

  @count = 0

  def subscribed
    # ...
    @count++
    stream_for(params[:url])
    transmit(number_of_users: nbUsers)
  end
  # ...
end


export default class OnSamePageSubscription extends React.Component {
  // ...
  received = (data) => {
    console.log(`Number of users: ${data.number_of_users}`);

    this.props.onUpdate(data);
  };
}
  • 以上是您问题的直接答案......

      

    如何调用此方法(nbUsers)并获得结果?

    ...但是,您当前的代码中仍然存在许多问题:

    • class OnTheSamePageChannel < ApplicationCable::Channel
        @count = 0 
        # ^ this is a class-level instance-variable
        # ...and not an instance-level instance-variable (that I think you were under the impression of
      end
      
      # To demonstrate:
      # OnTheSamePageChannel.new.instance_variable_get(:@count)
      # => nil
      # OnTheSamePageChannel.instance_variable_get(:@count)
      # => 0
      
    • 要解决此问题,您需要执行以下操作:

      class OnTheSamePageChannel < ApplicationCable::Channel
        attr_accessor :count
        @count = 0 
      
        def subscribed
          # ...
          self.class.count += 1
        end
      
        def unsubscribed 
          # ...
          self.class.count -= 1
        end
      
        def nbUsers
          # ...
          self.class.count
        end
      end
      
    • 重要提示:上面的代码仅适用于单进程服务器,如果您已经有多个服务器或进程,或其他依赖于rails的应用程序(如Sidekiq)或(rails-),它将无法完全正常工作控制台)正是因为内存(其中存储了@count)不在它们之间共享。据我所知,还没有(还有?)一种简单的内置Rails方法来检查连接用户的数量,如果&#34;缩放&#34;已经考虑到了。请随意查看其他SO question