gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:20:08 25 4
gpt4 key购买 nike

我在连接网络套接字时遇到问题。有错误:

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_CONNECTION: Upgrade, 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

连接.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'].user 解决了我的问题。以下是更新的方法。

   def find_verified_user
(current_user = env['warden'].user) ? current_user : reject_unauthorized_connection
end

关于ruby-on-rails - ActionCable - 无法升级到 WebSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39438568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com