在我的Ruby on Rails项目中,我有一个控制器InboxController
,该控制器具有一个动作conversation
,该动作与get
和{{1} }作为参数:
user_phone
我的目标是在前端显示有多少用户正在使用短代码short_code
和user_phone class InboxController < ApplicationController
def conversation
@short_code = params[:short_code]
@user_phone = params[:user_phone]
end
end
观看对话。也就是说,有多少用户正在实时查看与@short_code
关联的页面。
为此,我认为只有在设置了user_phone
和InboxController#conversation
之后,我才应该开始流式传输。这两个参数都是字符串。
我有一个@short_code
文件:
@user_phone
我的conversation_channel.rb
文件是:
class ConversationChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
if current_user && @short_code && @user_phone
byebug
puts "current_user is #{current_user} and short code is #{@short_code} and user_phone is #{@user_phone}"
stream_from "conversation_channel_#{@short_code+'&'+@user_phone}"
end
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def conversation
end
end
即使在conversation.coffee
中设置了App.conversation = App.cable.subscriptions.create "ConversationChannel",
connected: ->
# Called when the subscription is ready for use on the server
console.log('connected in conversation channel')
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
console.log('received in conversation channel')
conversation: ->
@perform 'conversation'
和ConversationChannel
,@short_code
中的byebug也永远不会被击中,并且我确保已设置它们,因为我在它。但是@user_phone
的设置正确,我可以仅通过InboxController#conversation
使用byebug来访问它。
在控制器代码中设置了current_user
和if current_user
之后,我尝试调用stream_from "conversation_channel_#{@short_code+'&'+@user_phone}"
,但由于#`而收到错误@short_code
stream_from'
我确定我的ActionCable设置正确,因为我有另一个频道并且该频道正常工作。
解决此问题的正确方法是什么?