ActionCable - подключен, но не подключен - PullRequest
0 голосов
/ 28 августа 2018

Я использую ActionCable с рельсами для создания уведомлений в реальном времени. Метод connect в ApplicationCable :: Connection оправдан, но обратного вызова на стороне клиента нет!

Это мои файлы:

приложение / каналы / connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private
      def find_verified_user
        if current_user = User.last
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

приложение / каналы / order_channel.rb

class OrderChannel < ApplicationCable::Channel
  def subscribed
    stream_from "order_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def speak_m(data)
    ActionCable.server.broadcast('order_channel', data)
  end
end

приложение / активы / JavaScripts / cable.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

приложение / активы / JavaScripts / кабель / подписок / order.coffee

App.order = App.cable.subscriptions.create "OrderChannel",
  connected: ->
    @speak_m()

    # Called when the subscription is ready for use on the server

  disconnected: ->
    alert 'disconnected disconnected 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

  speak_m: ->
    @perform("speak_m", 'My Message!!')

конфиг / application.rb

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Web
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    config.action_cable.mount_path = '/cable'

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
  end
end

конфиг / cable.yml

development:
  adapter: async

test:
  adapter: async

Я добавил <%= action_cable_meta_tag %> к макету

и добавлено mount ActionCable.server => '/cable' к rout.rb

Я использую: - Рубин 2.3 - рельсы 5.1.4 - пума 3,7 - postgres 9,6

Я использую докер

...