actioncable 无法连接到 websocket

Evo*_*tio 5 ruby ruby-on-rails ruby-on-rails-4 ruby-on-rails-5 actioncable

我无法连接到可操作的网络套接字。

我将此行添加到我的routes.rb中

match "/websocket", :to => ActionCable.server, via: [:get, :post]
Run Code Online (Sandbox Code Playgroud)

我将此文件添加到我的应用程序路径: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
Run Code Online (Sandbox Code Playgroud)

频道/application_cable/channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end
Run Code Online (Sandbox Code Playgroud)

通道/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
Run Code Online (Sandbox Code Playgroud)

和我的 javascriptfiles 下的 asset/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'
Run Code Online (Sandbox Code Playgroud)

然后我开始我的 Rails 应用程序:

TRUSTED_IP=192.168.0.0/16 rails s -b 192.168.56.101 Puma
Run Code Online (Sandbox Code Playgroud)

我的 ActionCalbe-Server 具有:

bundle exec puma -p 28080 cable/config.ru
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中打开应用程序时,出现以下错误:

Firefox kann keine Verbindung zu dem Server unter ws://localhost:28080/ aufbauen.
Firefox kann keine Verbindung zu dem Server unter ws://localhost/websocket aufbauen.
Run Code Online (Sandbox Code Playgroud)

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

aro*_*ero 2

你的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
Run Code Online (Sandbox Code Playgroud)