2017-02-01 7 views
1

私はアパートの宝石を持つSaaSプラットフォームを構築しており、チャット機能のためにActionCableを導入しています。これで Rails ActionCable Request Originsサブドメイン

アプリ/チャンネル/ connection.rbに
module ApplicationCable 
    class Connection < ActionCable::Connection::Base 
    identified_by :current_user 

    def connect 
     self.current_user = find_verified_user 
     tenant = request.subdomain 
     Apartment::Tenant.switch!(tenant) 
     logger.add_tags "ActionCable", "User #{current_user.id}", "Tenant #{tenant}" 
    end 

    protected 

     def find_verified_user 
     if current_user = env['warden'].user 
      current_user 
     else 
      reject_unauthorized_connection 
     end 
     end 
    end 
end 

Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) 
    User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["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 2017-02-01 08:49:53 -0600 
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-02-01 08:49:53 -0600 

私はこのコードを使用しています:私は、構築されたチャット機能のほとんどを持っているが、私は次の取得チャネルに送信するとき理論はテナントを切り替えて、適切なサブドメインでActioncableの送信を取得する必要があります。しかし、私はまだこれらのエラーを取得しています。

私はDeviseを使用しているので、ユーザーとしてログインしているので、現在のユーザーを検索し、ユーザーがログインしているユーザーであることを検証している理由が考えられます園長の

私は、特定の方法でリクエストの発信元を設定する必要がある場合、私はサブドメインを持っているので不思議です。これをテストする

私は私のようなlvh.meのローカルホストのドメインで使用している私のサブドメインの1つをハードコード:

Rails.application.config.action_cable.allowed_request_origins = ['http://acme.lvh.me:3000'] 

allowed_request_originsはこれとは何かを持っているし、もしそうならば、私は思っていましたどのように正規表現を使用してlvh.me:3000 url/domainspaceのサブドメインを許可することができますか?

お願いします。

答えて

0

友人の助けを借りて、接続を確立する前にテナントを切り替える必要があることを知りました。これまでのところ、それが働いていると、ACは上のアパートで正常に送信され

Rails.application.config.action_cable.allowed_request_origins = ['http:\/\/*.lvh.me:3000*'] 

:ここで私はこれをしなかった要求の起源を処理するdevelopment.rbで正規表現の部分を処理するためにconnection.rb

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

    def connect 
     tenant = request.subdomain 
     Apartment::Tenant.switch!(tenant) 
     self.current_user = find_verified_user 
     logger.add_tags "ActionCable", "User #{current_user.id}", "Tenant #{tenant}" 
    end 

    protected 

     def find_verified_user 
     if current_user = env['warden'].user 
      current_user 
     else 
      reject_unauthorized_connection 
     end 
     end 
    end 
end 

ですサブドメイン。少なくとも開発中。

+0

サブドメインでActionCableを使用しようとしています。ユーザーはチャンネルページを開くためにDeviseでサインインする必要があります。ルートドメインでは 'env ['warden']'はcurrent_userを提供しますが、サブドメインにあるときはActionCable.connectが 'env ['warden']' nilを見つけます。これはどうですか? – dlehman

関連する問題