Rails ActionCable "connected" 方法未被调用

Rails ActionCable "connected" method not being called

我正在尝试在我的 Rails 项目中使用 ActionCable,虽然似乎正在建立 WebSocket 连接,但没有发送任何消息(仅 ping)。

据我所知,我的 CoffeeScript 中的 connected 方法从未被调用过。

这是我的频道定义:

# app\channels\quiz_data_channel.rb:
class QuizDataChannel < ApplicationCable::Channel
  def subscribed
    stream_from "quiz_data"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

这是我的咖啡脚本:

#app\assets\javascripts\channels\quiz_data.coffee:
App.quiz_data = App.cable.subscriptions.create "QuizDataChannel",
  connected: ->
    # Called when the subscription is ready for use on the server
    console.log "[AC] on click handler called?"
    $('btn btn-primary btn-lg').on 'click', console.log "[AC] on click handler called!"


  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

有人知道问题出在哪里吗?

这是我的第一个 rails 项目,如有任何帮助,我们将不胜感激。

看看你的 quiz_data.coffee,你有 App.quiz_data = App.cable.subscriptions.create "QuizDataChannel"。我相信这会在您的 quiz_data_channel.rb 文件中出现 quiz_data_channel;您定义 stream_from "quiz_data" 的位置。尝试将其更改为 stream_from "quiz_data_channel"。让我知道你过得怎么样!