Actioncable 广播未达到 Received JS 功能
Actioncable broadcasts not hitting Received JS function
我一直在尝试整合一个 rails 应用程序来替换编码不当的 php 怪物。当前的化身是从非 Rails 数据库中提取数据,将其放入 Rails 数据库中,然后在视图中显示。数据库主要填充每隔几秒添加一次的温度读数。我可以在 Rails 中显示一个静态页面,没有任何问题,但是,尝试添加 ActionCable/realtime 数据被证明是有问题的。大多数事情似乎都在正常工作,但是,当我向我的频道广播时,它似乎没有达到 mychannel.coffee.
中的 Received 功能。
我的设置:
Server - passenger (bundle exec passenger start)
乔布斯 - Resque
ActionCable-Redis
数据是通过抓取原始 SQL 并创建新记录的作业从 legacydb 导入的。之后,另一个工作广播到频道。
我认为问题出在 ActionCable 上。看来,我能找到的所有示例都需要用户输入来触发 JS。但是,我试图从服务器端严格触发。这份工作:
class DatabroadcastJob < ApplicationJob
queue_as :default
self.queue_adapter = :resque
def perform
ActionCable.server.broadcast 'dashboard_channel', content: render_thedata
end
private
def render_thedata
dataArr =[Data1.last, Data2.last, Data3.last]
ApplicationController.renderer.render(partial:
'dashboard/data_tables', locals: {item: dataArr})
end
end
有效。有用。我看到广播达到 dashboard_channel。但是,dashboard.coffee 中的任何内容都不会被广播触发。这令人难以置信。
Dashboard.coffee
App.dashboard = App.cable.subscriptions.create "DashboardChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
alert data['content']
没有任何反应。日志显示了广播,但没有命中 dashboard.coffee 并在浏览器中引发警报。由于所有聊天示例,我是否以错误的方式思考这个问题?是否有另一个地方可以让我在仅进行服务器端更改时获取广播并将其推送给订阅者?
如果需要任何其他信息来解决此问题,请告诉我。几天来,这个问题一直困扰着我。
首先,检查你的框架。您确定收到了您想要的消息吗?
然后,您应该在您的频道中为您的订阅者设置一个 ID。如果你有一个与模型相关的流,那么使用的广播可以从模型和频道生成。
class DashboardChannel < ApplicationCable::Channel
def subscribed
post = Post.find(params[:id])
stream_for post
end
end
然后你就可以像这样向你的频道广播了
DashboardChannel.broadcast_to(@post, @comment)
否则,您应该执行以下操作:
class DashboardChannel < ApplicationCable::Channel
def subscribed
stream_from 'dashboard_channel'
end
end
但这是一种不好的做法,因为您将无法定义将哪个用户传输到您的服务器。
为了对 coffee/javascript 进行故障排除和测试,我要补充的一件事是 console.log
是你的朋友。在整个过程中添加 console.log "First step complete"
等等确实有助于追踪错误发生的位置。
我一直在尝试整合一个 rails 应用程序来替换编码不当的 php 怪物。当前的化身是从非 Rails 数据库中提取数据,将其放入 Rails 数据库中,然后在视图中显示。数据库主要填充每隔几秒添加一次的温度读数。我可以在 Rails 中显示一个静态页面,没有任何问题,但是,尝试添加 ActionCable/realtime 数据被证明是有问题的。大多数事情似乎都在正常工作,但是,当我向我的频道广播时,它似乎没有达到 mychannel.coffee.
中的 Received 功能。我的设置: Server - passenger (bundle exec passenger start) 乔布斯 - Resque ActionCable-Redis
数据是通过抓取原始 SQL 并创建新记录的作业从 legacydb 导入的。之后,另一个工作广播到频道。
我认为问题出在 ActionCable 上。看来,我能找到的所有示例都需要用户输入来触发 JS。但是,我试图从服务器端严格触发。这份工作:
class DatabroadcastJob < ApplicationJob
queue_as :default
self.queue_adapter = :resque
def perform
ActionCable.server.broadcast 'dashboard_channel', content: render_thedata
end
private
def render_thedata
dataArr =[Data1.last, Data2.last, Data3.last]
ApplicationController.renderer.render(partial:
'dashboard/data_tables', locals: {item: dataArr})
end
end
有效。有用。我看到广播达到 dashboard_channel。但是,dashboard.coffee 中的任何内容都不会被广播触发。这令人难以置信。
Dashboard.coffee
App.dashboard = App.cable.subscriptions.create "DashboardChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
alert data['content']
没有任何反应。日志显示了广播,但没有命中 dashboard.coffee 并在浏览器中引发警报。由于所有聊天示例,我是否以错误的方式思考这个问题?是否有另一个地方可以让我在仅进行服务器端更改时获取广播并将其推送给订阅者?
如果需要任何其他信息来解决此问题,请告诉我。几天来,这个问题一直困扰着我。
首先,检查你的框架。您确定收到了您想要的消息吗?
然后,您应该在您的频道中为您的订阅者设置一个 ID。如果你有一个与模型相关的流,那么使用的广播可以从模型和频道生成。
class DashboardChannel < ApplicationCable::Channel
def subscribed
post = Post.find(params[:id])
stream_for post
end
end
然后你就可以像这样向你的频道广播了
DashboardChannel.broadcast_to(@post, @comment)
否则,您应该执行以下操作:
class DashboardChannel < ApplicationCable::Channel
def subscribed
stream_from 'dashboard_channel'
end
end
但这是一种不好的做法,因为您将无法定义将哪个用户传输到您的服务器。
为了对 coffee/javascript 进行故障排除和测试,我要补充的一件事是 console.log
是你的朋友。在整个过程中添加 console.log "First step complete"
等等确实有助于追踪错误发生的位置。