javascript - ActionCable channel 示例 1 : User appearances. 循环?

标签 javascript ruby-on-rails actioncable

我目前正在尝试最全面地了解 ActionCable。有人能解释一下官方 doc 中的示例代码到底发生了什么吗? :

# app/channels/appearance_channel.rb
class AppearanceChannel < ApplicationCable::Channel
  def subscribed
    current_user.appear
  end

  def unsubscribed
    current_user.disappear
  end

  def appear(data)
    current_user.appear on: data['appearing_on']
  end

  def away
    current_user.away
  end
end

连同:

# app/assets/javascripts/cable/subscriptions/appearance.coffee
App.cable.subscriptions.create "AppearanceChannel",
  # Called when the subscription is ready for use on the server
  connected: ->
    @install()
    @appear()

  # Called when the WebSocket connection is closed
  disconnected: ->
    @uninstall()

  # Called when the subscription is rejected by the server
  rejected: ->
    @uninstall()

  appear: ->
    # Calls `AppearanceChannel#appear(data)` on the server
    @perform("appear", appearing_on: $("main").data("appearing-on"))

  away: ->
    # Calls `AppearanceChannel#away` on the server
    @perform("away")


  buttonSelector = "[data-behavior~=appear_away]"

  install: ->
    $(document).on "turbolinks:load.appearance", =>
      @appear()

    $(document).on "click.appearance", buttonSelector, =>
      @away()
      false

    $(buttonSelector).show()

  uninstall: ->
    $(document).off(".appearance")
    $(buttonSelector).hide()

我不确定的是,如果 current_user.appear 创建了一个循环并因此告诉我用户已通过从客户端到服务器并返回的 ping 登录?服务器端显示功能中的“on:”主题标签有什么作用? 提前致谢。

最佳答案

从客户端-> 服务器-> 客户端的循环来看,您是正确的。

更详细地说,当连接到 channel 时,@appear 函数被调用。我们可以在该函数中看到,它使用 @perform 调用名为 appear 的服务器端函数。不幸的是,在此之后它很模糊,但假设我们想向所有用户广播回这个人现在在线。

可能发生的情况的一个例子是 User 模型上的 appear 函数在用户对象上设置了一个 bool 值以指示他们在线,并使用 on 参数如下:

# models/user.rb
def appear(data)
  self.update(online: true, current_room: data['on'])
end

在此之后,我们需要一种方法让其他用户知道此人现在在线。所以首先我们必须广播这个,这可能会在更新后发生(有更好的地方可以放置它,但为了解释数据流,这就足够了):

# models/user.rb
def appear(data)
  self.update(online: true, current_room: data['on'])
  ActionCable.server.broadcast "AppearanceChannel", {event: 'appear', user_id: self.id, room: self.current_room}
end

所以现在连接到外观 channel 的所有用户都会收到数据,所以我们可以在前端添加这个。假设我们只想获取某种包含用户信息的 div,如果他们在线,则给他们一个类 online,否则删除该类:

received: (data) ->
  userId = data.user_id
  eventType = data.event
  if eventType == 'appear'
    $('#user_' + userId).addClass 'online'
  else
    $('#user_' + userId).removeClass 'online'

所以现在它会更新所有连接到 channel 的用户,告诉他们这个用户现在在线。

请注意,我们没有使用用户当前所在的房间,但如果需要,我们可以使用 data.room 获取它,然后根据需要使用它。

希望这有助于澄清事情。

关于javascript - ActionCable channel 示例 1 : User appearances. 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44184899/

相关文章:

javascript - 在 Angular 5 中使用 Observable.ForkJoin 重复 Http 请求

iPhone CPU 使用率上的 Javascript SVG 动画

php - 如何在禁用浏览器的 javascript 时验证表单数据?

c# - 防止通过 htmlAttributes 分配给 Html.TextBoxFor 的 onchange 事件的 javascript 编码

ruby-on-rails - 使用redirect_to添加url参数

ruby-on-rails - ActionCable 不在后台作业 rails 中运行 5

ruby-on-rails - 无法通过远程源连接到 Action Cable

ruby-on-rails - ruby rails : Problem adding Transient Attribute to Object for JSON Serializaton

javascript - Rails : format. js 或 format.json,或两者兼而有之?

ruby-on-rails-4 - ActionCable 连续连接/断开环路