Rails ActionCable 6: сбой подключения WebSocket к 'ws: // localhost: 3000 / cable': WebSocket закрыт из-за приостановки - PullRequest
0 голосов
/ 08 апреля 2020

Ванильное приложение Rails 6 выдает эту ошибку:

"WebSocket connection to 'ws://localhost:3000/cable' failed: WebSocket is closed due to suspension"

Канал Actioncable:

#app/javascript/channels/chat_channel.js
import consumer from "./consumer"

consumer.subscriptions.create("ChatChannel", {
  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) {
    console.log(data)
    // Called when there's incoming data on the websocket for this channel
  }
});

Контроллер Rails:

 #app/controller/messages_controller:
 puts "*******Broadcasting"
 ActionCable.server.broadcast("chat_channel", content: @message.id)

Канал ActionCable:

# app/channele/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
  def subscribed
    puts "**** ChatChannel Subscribed"
    stream_from "chat_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

Куда я подскользнулся?

1 Ответ

1 голос
/ 08 апреля 2020

Убедитесь, что в вашем rout.rb есть этот mount ActionCable.server => '/cable'

И в вашем development.rb файле

config.action_cable.url = "ws://localhost:3000/cable"

config.action_cable.allowed_request_origins = [/http:\/\/*/, 
/https:\/\/*/]
...