【问题标题】:Rails ActionCable "connected" method not being called未调用 Rails ActionCable“已连接”方法
【发布时间】:2017-11-25 11:20:06
【问题描述】:

我正在尝试在我的 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 项目,我们将不胜感激。

【问题讨论】:

    标签: ruby-on-rails websocket coffeescript actioncable


    【解决方案1】:

    看着你的quiz_data.coffee,你有 App.quiz_data = App.cable.subscriptions.create "QuizDataChannel"。我相信这是在你的quiz_data_channel.rbfile 中期待quiz_data_channel;你在哪里定义了stream_from "quiz_data"。尝试将其更改为 stream_from "quiz_data_channel"。告诉我你过得怎么样!

    【讨论】:

    • 是的,谢谢,更改它修复了它。一开始我只是没有注意到,因为我正在查看 windows 控制台,而不是 browsertools 控制台>_
    猜你喜欢
    • 2021-09-29
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2016-04-08
    • 2017-01-23
    • 2017-11-17
    相关资源
    最近更新 更多