【问题标题】:What is the list of available callbacks for ActionCable subscription create method?ActionCable 订阅创建方法的可用回调列表是什么?
【发布时间】:2018-03-02 11:35:59
【问题描述】:

我正在尝试设置我的 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


    【解决方案1】:

    从源代码来看,这是仅有的三个开箱即用的回调:

    connected

    received

    disconnected

    【讨论】:

      【解决方案2】:

      在你指向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 语法,可以更富有表现力地模块化您的代码。

      还有多少?

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

      // 更新:

      完整列表可以在here找到。

      【讨论】:

      • 这甚至没有尝试回答这个问题。
      猜你喜欢
      • 2017-12-19
      • 2017-03-28
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      相关资源
      最近更新 更多