6
웹 소켓에 연결하는 데 문제가 있습니다. 이 오류가 :ActionCable - WebSocket으로 업그레이드하지 못했습니다.
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
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]가 nil이라는 것을 알았습니다. 이 문제의 원인은 무엇입니까? 테스트를 위해 표준 URL과 포트를 사용합니다 (localhost : 3000).
충분하지 않습니다. 이 방법은 사용자가 로그인 할 때 오류를 제거한 것입니다. 그러나 사용자가 로그인하지 않은 경우 ** 오류가 지속됩니다 **. 이는 App.room = App.cable.subscriptions.create "RoomChannel"'room.coffee' (또는 코드에서와 같이)에서 호출하기 때문입니다. 이 솔루션은 사용자가 로그인했는지 여부에 따라'room.coffee'를 조건부로 렌더링하는 것이지만 자산에서'room.coffee'를 제외시켜야하고 별도의 렌더링이 필요합니다 (조건부로 js를 렌더링 할 수 없기 때문에 if 사용자가 로그인/로그 아웃했습니다). – prograils