ruby-on-rails – ActionCable – 无法升级到WebSocket

我有连接到Web套接字的问题.有一个错误
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,HTTP_CONNECTION: Upgrade,HTTP_UPGRADE: websocket)
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ?  [["LIMIT",1]]
An unauthorized connection attempt was rejected
Failed to upgrade to WebSocket (REQUEST_METHOD: GET,HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-09-11 18:57:49 +0200
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-09-11 18:57:49 +0200

connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable',"User #{current_user.id}"
    end

    protected

    def find_verified_user
      if verified_user = User.find_by(id: cookies.signed[:user_id])
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

我找到了一些我应该使用config.allowed_request_origins的解决方案,但它无法解决我的问题.我通过添加方法尝试使用session_helper:

def set_cookie(user)
  the_username = user.username.to_s
  cookies.permanent.signed[:username] = the_username
end

没有什么能解决我的问题.

更新:
我看到问题是cookies.signed [:user_id]是零.你有什么建议可以解释这个原因吗?我使用标准URL和端口进行测试(localhost:3000).

解决方法

我使用env [‘warden’].用户解决了我的问题.以下是更新方法.
def find_verified_user
      (current_user = env['warden'].user) ? current_user : reject_unauthorized_connection
   end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...