我现在已经尝试了多种方法,但似乎无法获得要广播给客户端的消息。
在这个应用程序中,我获得了一个执行通道订阅的react组件。我的连接应用程序的控制台输出显示在我的日志中,但接收到的只是没有被调用。
我的React组件:
# app/assets/javascripts/components/records.js.coffee
App.record = null
@Records = React.createClass
getInitialState: ->
@setupSubscription()
records: @props.data
getDefaultProps: ->
records: []
setupSubscription: ->
App.record = App.cable.subscriptions.create "RecordChannel",
connected: ->
console.log "connected to RecordChannel"
# Timeout here is needed to make sure Subscription
# is setup properly, before we do any actions.
#setTimeout: ->
# this.perform 'follow',
# {message_id: this.message_id}
# ,1000
received: (data) ->
console.log "data received"
@addRecord(data)
#record_subs = App.record
addRecord: (record) ->
records = React.addons.update(@state.records, { $push: [record] })
@setState records: records
deleteRecord: (record) ->
index = @state.records.indexOf record
records = React.addons.update(@state.records, { $splice: [[index,1]] })
@replaceState records: records
updateRecord: (record,data) ->
index = @state.records.indexOf record
records = React.addons.update(@state.records, { $splice: [[index,1,data]] })
@replaceState records: records
credits: ->
credits = @state.records.filter (val) -> val.amount >= 0
credits.reduce ((prev, curr) ->
prev + parseFloat(curr.amount)
), 0
debits: ->
debits = @state.records.filter (val) -> val.amount < 0
debits.reduce ((prev, curr) ->
prev + parseFloat(curr.amount)
), 0
balance: ->
@debits() + @credits()
render: ->
React.DOM.div
className: 'records'
React.DOM.h2
className: 'title'
'Records'
React.DOM.div
className: 'row'
React.createElement AmountBox, type: 'success', amount: @credits(), text: 'Credit'
React.createElement AmountBox, type: 'danger', amount: @debits(), text: 'Debit'
React.createElement AmountBox, type: 'info', amount: @balance(), text: 'Balance'
React.createElement RecordForm, handleNewRecord: @addRecord
React.DOM.hr null
React.DOM.table
className: 'table table-bordered'
React.DOM.thead null,
React.DOM.tr null,
React.DOM.th null, 'Date'
React.DOM.th null, 'Title'
React.DOM.th null, 'Amount'
React.DOM.th null, 'Actions'
React.DOM.tbody null,
for record in @state.records
React.createElement Record, key: record._id["$oid"], record: record, handleDeleteRecord: @deleteRecord, handleEditRecord: @updateRecord不确定你还需要看哪段代码,请让我知道,我会把它贴出来。
发布于 2018-02-20 05:44:16
我终于找出了我的应用程序中的错误所在。我的订阅方法在堆栈中是错误的。原来我在订阅中使用的是stream_for而不是stream_for!
class RecordChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_for ("record_#{current_user['info']['nickname']}_channel")
end至
class RecordChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_from ("record_#{current_user['info']['nickname']}_channel")
endhttps://stackoverflow.com/questions/48857369
复制相似问题