仅当调用控制器动作时,ActionCable才连接到通道

时间:2018-10-01 14:27:31

标签: ruby-on-rails model-view-controller websocket coffeescript actioncable

在我的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_phoneInboxController#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_userif current_user之后,我尝试调用stream_from "conversation_channel_#{@short_code+'&'+@user_phone}",但由于#`而收到错误@short_code stream_from'

我确定我的ActionCable设置正确,因为我有另一个频道并且该频道正常工作。

解决此问题的正确方法是什么?

0 个答案:

没有答案