actioncable 无法连接到 websocket

actioncable can't connect to websocket

我无法连接到 actioncable websocket。

我将这一行添加到我的 routes.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

channels/application_cable/channel.rb

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

channels/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.咖啡

#= 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.

如何解决我的问题?我没有发现错误:/

你的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