首页 文章

actioncable无法连接到websocket

提问于
浏览
3

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

和我的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.

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

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
    

相关问题