5

我无法连接到 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

频道/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

和我在 assets/javascript/channels/index.coffee 下的 javascriptfiles

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

我怎样才能解决我的问题?我没有发现错误:/

4

1 回答 1

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
于 2015-11-15T15:29:24.037 回答