【问题标题】:ActionCable - how to display number of connected users?ActionCable - 如何显示连接用户的数量?
【发布时间】:2015-12-30 13:13:49
【问题描述】:

我正在尝试使用 Action Cable 创建一个简单的类似聊天的应用程序(计划扑克应用程序)。我对术语、文件层次结构和回调的工作方式有点困惑。

这是创建用户会话的操作:

class SessionsController < ApplicationController
  def create
    cookies.signed[:username] = params[:session][:username]
    redirect_to votes_path
  end
end

然后用户可以发布应该广播给所有人的投票:

class VotesController < ApplicationController
  def create
    ActionCable.server.broadcast 'poker',
                                 vote: params[:vote][:body],
                                 username: cookies.signed[:username]
    head :ok
  end
end

到目前为止,一切对我来说都很清楚并且工作正常。问题是 - 我如何显示连接用户的数量?当用户(消费者?)连接时,是否有在 JS 中触发的回调?我想要的是当我以隐身模式在 3 个不同的浏览器中打开 3 个选项卡时,我想显示“3”。当一个新用户连接时,我希望这个数字增加。如果任何用户断开连接,则该数字应递减。

我的PokerChannel

class PokerChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'poker'
  end
end

app/assets/javascripts/poker.coffee:

App.poker = App.cable.subscriptions.create 'PokerChannel',

  received: (data) ->
    $('#votes').append @renderMessage(data)

  renderMessage: (data) ->
    "<p><b>[#{data.username}]:</b> #{data.vote}</p>"

【问题讨论】:

    标签: ruby-on-rails websocket actioncable


    【解决方案1】:

    我想我为你找到了答案。 试试这个:

    ActionCable.server.connections.select { |con| con.current_room == room }.length?
    

    我可以在我的代码中的任何地方使用它,并检查连接到所选流的用户数量 :)

    在我的connection.rb 我有这样的东西:

    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_room
    
        def connect
          self.current_room = find_room
        end
    
        private
    
        def find_room
          .....
        end
      end
    end
    

    希望对任何人都有帮助。

    【讨论】:

      【解决方案2】:

      ActionCable.server.pubsub.send(:listener).instance_variable_get("@subscribers")
      

      您可以在将在广播中执行的程序的键和数组中获取带有订阅标识符的映射。所有过程都接受消息作为参数并具有记忆连接。

      【讨论】:

        【解决方案3】:

        似乎一种方法是使用

        ActionCable.server.connections.length
        

        (请参阅 cmets 中的注意事项)

        【讨论】:

        • 请注意,这是调用的特定线程的当前连接数,而不是各种进程和线程的连接数
        • 这也是与服务器的连接数,而不是与特定频道的连接数。
        【解决方案4】:

        在一个相关的question on who is connected中,有一个使用redis的人的答案:

        Redis.new.pubsub("channels", "action_cable/*")
        

        如果你只想要连接数:

        Redis.new.pubsub("NUMPAT", "action_cable/*")
        

        这将汇总来自所有服务器的连接。

        RemoteConnections 类和 InternalChannel 模块中涵盖的所有魔法。

        TL;DR 以 action_cable/* 为前缀的特殊通道上订阅的所有连接,仅用于断开套接字与主轨道应用程序的连接。

        【讨论】:

          【解决方案5】:

          对于快速(可能并不理想)的解决方案,您可以编写一个跟踪订阅计数的模块(使用 Redis 存储数据):

          #app/lib/subscriber_tracker.rb
          module SubscriberTracker
            #add a subscriber to a Chat rooms channel 
            def self.add_sub(room)
              count = sub_count(room)
              $redis.set(room, count + 1)
            end
          
            def self.remove_sub(room)
              count = sub_count(room)
              if count == 1
                $redis.del(room)
              else
                $redis.set(room, count - 1)
              end
            end
          
            def self.sub_count(room)
              $redis.get(room).to_i
            end
          end
          

          并在频道类中更新您订阅和取消订阅的方法:

          class ChatRoomsChannel < ApplicationCable::Channel  
            def subscribed
               SubscriberTracker.add_sub params['room_id']
            end
          
            def unsubscribed
               SubscriberTracker.remove_sub params['chat_room_id'] 
            end
          end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-03-22
            • 1970-01-01
            • 2019-04-11
            • 2020-07-30
            • 2019-10-23
            • 2020-11-09
            • 2016-07-06
            • 1970-01-01
            相关资源
            最近更新 更多