gpt4 book ai didi

ruby-on-rails - Actioncable 广播未命中 Received JS 函数

转载 作者:行者123 更新时间:2023-12-02 20:00:23 24 4
gpt4 key购买 nike

我一直在尝试制作一个 Rails 应用程序来取代编码困惑的 php 怪物。当前的形式是从非 Rails 数据库中提取数据,将其放入 Rails 数据库中,然后在 View 中显示。数据库主要填充每隔几秒添加一次的温度读数。我可以毫无问题地在 Rails 中显示静态页面,但是尝试添加 ActionCable/实时数据已被证明是有问题的。大多数事情似乎都工作正常,但是当我广播到我的 channel 时,它似乎没有达到 mychannel.coffee 中的 Received 功能。

我的设置:服务器 - 乘客(捆绑执行乘客启动)工作机会 - 瑞斯克ActionCable - Redis

数据是通过抓取原始 SQL 并创建新记录的作业从legacydb 导入的。此后,另一个作业将广播到该 channel 。

我认为问题来自于 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

有效。有用。我看到广播到达了仪表板 channel 。但是,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并在浏览器中引发警报。由于所有的聊天示例,我是否以错误的方式思考这个问题?当仅进行服务器端更改时,是否还有其他地方可以获取广播并将其推送给订阅者?

如果需要任何其他信息来解决此问题,请告诉我。这个问题几天来一直困扰着我。

最佳答案

首先,检查您的框架。您确定收到了您想要的消息吗?

enter image description here

然后,您应该在您的 channel 中为您的订阅者设置一个 ID。如果您有与模型​​相关的流,则可以从模型和 channel 生成所使用的广播。

class DashboardChannel < ApplicationCable::Channel
def subscribed
post = Post.find(params[:id])
stream_for post
end
end

然后您就可以像这样广播到您的 channel

DashboardChannel.broadcast_to(@post, @comment)

否则,您应该执行以下操作:

class DashboardChannel < ApplicationCable::Channel
def subscribed
stream_from 'dashboard_channel'
end
end

但这是一个不好的做法,因为您将无法定义哪个用户传输到您的服务器。

关于ruby-on-rails - Actioncable 广播未命中 Received JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com