我正在尝试设置我的Rails 5 ActionCable
来广播我的数据库的更新。到目前为止,我已经开始了,但我意识到ActionCable上的文档有点缺乏。对于我的情况,我想知道我被允许放入函数subscriptions.create()
的回调列表。
例如
const consumer = ActionCable.createConsumer();
consumer.subscriptions.create(
'ChatsChannel'
{
received: someCallback,
connected: otherCallback,
disconnected: anotherCallback
}
)
我注意到有来自的appendLine
和createLine
第5.4节http://guides.rubyonrails.org/action_cable_overview.html
还有多少人?它们对应的是什么?这与Node.js和Python上通常的websocket有很大的不同。使用socket.io,我只获得4个选项,open
,close
,error
和message
。为什么当Rails被认为是对配置的约定时,ActionCable
看起来如此不同寻常?
谢谢
在你指向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>
"""
在这段代码中,appendLine
和createLine
只是那里定义的回调,分别由@appendLine
和@createLine
指出。这只是CoffeeScript语法,可以更加明确地模拟您的代码。
还有多少人?
老实说,我不确定。我没有找到任何JS文档,如the ruby documentation of Action电缆那样广泛。但是appendLine
和createLine
没有定义回调,它们只出现在那个例子中。
//更新:
完整列表可以找到here。