我有一个订阅用户的ActionCable方法。如果一个新的convo被启动,我想要订阅用户到新的频道。我无法弄清楚在控制器中调用通道方法的正确语法。如何在一个轨道控制器中调用通道方法?
更新:问题是消息在发送时附加到聊天室,但是当发送第一条消息时,websocket连接尚未建立,因此它看起来像用户一样,如果消息不是发送(因为它没有被追加)。
通道/ 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控制器,创建方法,我已经试过几件事情:
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
封邮件的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