ActionCable订阅创建方法的可用回调列表是什么?

问题描述 投票:3回答:2

我正在尝试设置我的Rails 5 ActionCable来广播我的数据库的更新。到目前为止,我已经开始了,但我意识到ActionCable上的文档有点缺乏。对于我的情况,我想知道我被允许放入函数subscriptions.create()的回调列表。

例如

const consumer = ActionCable.createConsumer();
consumer.subscriptions.create(
    'ChatsChannel'
    {
        received: someCallback,
        connected: otherCallback,
        disconnected: anotherCallback
    }
 )

我注意到有来自的appendLinecreateLine

第5.4节http://guides.rubyonrails.org/action_cable_overview.html

还有多少人?它们对应的是什么?这与Node.js和Python上通常的websocket有很大的不同。使用socket.io,我只获得4个选项,opencloseerrormessage。为什么当Rails被认为是对配置的约定时,ActionCable看起来如此不同寻常?

谢谢

ruby-on-rails-5 actioncable
2个回答
1
投票

从源代码来看,这些是开箱即用的唯一三个回调:

connected

received

disconnected


0
投票

在你指向guide的部分中:

# app/assets/javascripts/cable/subscriptions/chat.coffee
App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
  received: (data) ->
    @appendLine(data)

  appendLine: (data) ->
    html = @createLine(data)
    $("[data-chat-room='Best Room']").append(html)

  createLine: (data) ->
    """
    <article class="chat-line">
      <span class="speaker">#{data["sent_by"]}</span>
      <span class="body">#{data["body"]}</span>
    </article>
    """

在这段代码中,appendLinecreateLine只是那里定义的回调,分别由@appendLine@createLine指出。这只是CoffeeScript语法,可以更加明确地模拟您的代码。

还有多少人?

老实说,我不确定。我没有找到任何JS文档,如the ruby documentation of Action电缆那样广泛。但是appendLinecreateLine没有定义回调,它们只出现在那个例子中。

//更新:

完整列表可以找到here

© www.soinside.com 2019 - 2024. All rights reserved.