私はユーザーを購読するActionCableメソッドを持っています。新しいconvoが開始された場合、私は新しいチャンネルにもユーザを登録したいと思います。私はコントローラでチャネルメソッドを呼び出すための適切な構文を理解することはできません。レールコントローラでチャンネルメソッドを呼び出すにはどうすればよいですか?
UPDATE:メッセージが送信されたときにチャットボックスに追加されますが、最初のメッセージが送信されたときにWebSocket接続がまだ確立されていないため、送信されます(追加されていないため)。私convosコントローラで
チャネル/ msgs_channel.rb
class MsgsChannel < ApplicationCable::Channel
#This function subscribes the user to all existing convos
def subscribed
@convos = Convo.where("sender_id = ? OR recipient_id = ?", current_user, current_user)
@convos.each do |convo|
stream_from "msg_channel_#{convo.id}"
end
end
#This is a new function I wrote to subscribe the user to a new convo that is started during their session.
def subscribe(convo_id)
stream_from "msg_channel_#{convo_id}"
end
end
、メソッドを作成し、私はいくつかのことを試してみました:
convos_controller.rb
def create
@convo = Convo.create!({sender_id: @sender_id, recipient_id: @recipient_id})
ActionCable.server.subscribe(@convo.id)
end
ActionCable.subscribe(@convo.id)
エラー: NoMethodError (undefined method
購読」ActionCableのために:モジュール) `
ActionCable.msgs.subscribe(@convo.id)
エラー: NoMethodError (undefined method
MSGのActionCable用:モジュール):`
App.msgs.subscribe(@convo.id)
エラー:NameError (uninitialized constant ConvosController::App):
MsgsChannel.subscribe(@convo.id)
はエラー:NoMethodError (undefined method
'MsgsChannelのために:Class`
ActionCable.server.subscribe(@convo.id)
エラー:NoMethodError (undefined method
購読' サブスクライブ#用): `
idに基づいて会話ごとにwebsocketを試していますか? –
@TallPaulそれは正しいです – gwalshington