3

我無法連接到actioncable的WebSocket。actioncable無法連接到WebSocket的

我加入這行到我的routes.rb

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

,我加入這個文件到我的應用程序路徑: 有線/ 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 

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 

和我javascriptfiles下 資產/ JavaScript的/渠道/ 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-服務器:

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

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