actioncable не может подключиться к веб-сокету

Я не могу подключиться к веб-сокету actioncable.

Я добавил эту строку в свой route.rb

match "/websocket", :to => ActionCable.server, via: [:get, :post]

и я добавил эти файлы в путь к моему приложению: cable/config.ru

# cable/config.ru
require ::File.expand_path('../../config/environment',  __FILE__)
Rails.application.eager_load!

require 'action_cable/process/logging'

run ActionCable.server

каналы/application_cable/channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

каналы/application_cable/connection.rb

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

    def connect
      self.current_user = find_verified_user
    end

    protected
      def find_verified_user
        if current_user
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

и мои javascript-файлы в assets/javascript/channels/index.coffee

#= require cable
#= require_self
#= require_tree .

@App = {}
# App.cable = Cable.createConsumer 'ws://localhost:28080'
# App.cable = Cable.createConsumer 'ws://localhost/websocket'

затем я запустил свое приложение rails с помощью:

TRUSTED_IP=192.168.0.0/16 rails s -b 192.168.56.101 Puma

и мой ActionCalbe-Server с:

bundle exec puma -p 28080 cable/config.ru

Когда я открыл свое приложение в браузере, я получаю следующие ошибки:

Firefox kann keine Verbindung zu dem Server unter ws://localhost:28080/ aufbauen.
Firefox kann keine Verbindung zu dem Server unter ws://localhost/websocket aufbauen.

Как я могу решить свою проблему? Я не нашел ошибку :/


person Evolutio    schedule 28.10.2015    source источник


Ответы (1)


Ваш метод find_verified_user не имеет смысла, он всегда будет терпеть неудачу. Измените его, чтобы найти пользователя на основе наличия некоторого (подписанного) файла cookie.

def find_verified_user
  if user = User.find_by(id: cookies.signed[:user_id])
    user
  else
    reject_unauthorized_connection
  end
end
person aromero    schedule 15.11.2015