2

我在我的机器上运行 ActionCable 时遇到问题。

在 assets/javascripts/channels 我有两个文件:

index.coffee

#= require action_cable
#= require_self
#= require_tree .

@App = {}
App.cable = ActionCable.createConsumer()

和 question.coffee

App.question = App.cable.subscriptions.create "QuestionChannel",
  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

  follow: ->
    @perform 'follow'

  unfollow: ->
    @perform 'unfollow'

我的 application.js 文件如下所示:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require icheck
//= require homer/scripts
//= require Chart
//= require excanvas
//= require channels
//= require_tree .

当我启动电缆服务器和 Rails 服务器并在 Firefox 控制台中访问 localhost:3000 时,我看到两个错误:

SyntaxError: An invalid or illegal string was specified


this.webSocket = new WebSocket(this.consumer.url);

TypeError: App.cable is undefined


App.question = App.cable.subscriptions.create("QuestionChannel", {

我正在使用 rails 5 beta 3。我该如何解决这个问题?

4

3 回答 3

1

确保你有

<%= action_cable_meta_tag %>

在您的应用程序布局中的某个位置。

默认情况下,Rails 会生成并负责将电缆 URL 提供给 Action Cable,这样您就不必将其传递给

ActionCable.createConsumer()

.

于 2016-04-05T12:11:26.223 回答
1

ActionCable.createConsumer需要一个参数 - Action Cable 服务器的地址。

@App = {}
App.cable = ActionCable.createConsumer("ws://cable.example.com")

如果你错过了这个参数,那么this.customer.url是未定义的。

于 2016-03-03T19:38:56.190 回答
0

我遇到过同样的问题。我将服务器更改为 puma whaaala 它有效!

您可以通过添加gem 'puma'并运行来更改bundle

另外,请确保您config.action_cable.url = "ws://localhost:3000/cable"在 development.rb 文件中。祝你好运

于 2019-01-07T12:06:30.167 回答