【问题标题】:Information about the unsubscribed channel stream in actioncableactioncable中未订阅频道流的信息
【发布时间】:2020-12-12 23:30:08
【问题描述】:

在 Rails 中实现 ActionCable 通道。

def subscribed

  room = Room.find_by_id(params[:room_id])
  stream_for room
end

def unsubscribed
  room_id = room.try(:id) || params[:room_id] 
end

可以在unsubscribed方法中获取room_id或者room(ActiveRecord obj)信息吗?

【问题讨论】:

  • 尝试在订阅状态下设置@room_id=params[:room_id],你也许可以在未订阅状态下使用@room_id
  • @F.E.A 谢谢,它正在工作

标签: ruby-on-rails websocket actioncable unsubscribe


【解决方案1】:

subscribed方法中使用instance(@)变量,我们可以在unsubscribed方法中得到相同的对象

def subscribed
  @room = Room.find_by_id(params[:room_id])
  stream_for @room
end

def unsubscribed
  puts "room => #{@room}" # Here we will get the same room object
end

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2020-11-22
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多