2016-05-10 10 views
2

ユーザモデル、セッションパラメータ、およびその他の情報を保持するServiceClassのインスタンスであるcurrent_userなどを使用するとします。 webSocketとの接続中に変数が設定され、異なるサブスクリプションのすべてのACコールで再利用されています。アクションケーブルを使用して状態を一貫させる方法

その後、ユーザーが自分のユーザー名を更新することを決定すると、current_user.update(new_username)が呼び出され、問題なく動作します。

しかし、そのユーザーの下の他のACサブスクリプションでも古いユーザーモデルが使用されています。私は各サブスクリプションが独自のスレッドの下で動作するので、あるスレッドの下でユーザーモデルを更新すると、他のスレッドの下でそれらを更新しないと思います。そのような場合の最良のアプローチは何ですか?

class ServiceClass 

def initialize(session,...) 
    @session = session 
    @user = current_user 
end 

def update!(username) 
    @user.username = username 
    @user.save! 
end 

... 
end 

module ApplicationCable 
    class Channel < ActionCable::Channel::Base 
     def current_user 
     @current_user ||= ServiceClass.new(session, user) 
     end 
    end 
end 

答えて

0

あなたがしたいことは、current_userにブロードキャストして、current_userが自分のストリームにサブスクライブすることです。お使いのコントローラで

ActionCable.server.broadcast "#{current_user}" 

自分のチャンネルで(。たとえば、アプリ/チャンネル/ user_channel.rb)

class UserChannel < ApplicationCable::Channel 
    def subscribed 
    stream_from "#{current_user}" 
    end 
end 
関連する問題