Rails 5 ActionCable 修改 confirm_subscription

Rails 5 ActionCable modify confirm_subscription

我得到了这个频道:

class TestChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'test'

    ActionCable.server.broadcast 'Test', vehicles: Vehice.find_by id: 13
  end
end

我不想在有人订阅时向所有订阅者广播。 我正在考虑通过添加 'message'.

来更改 confirm_subscription

有人能告诉我这是否可能吗?

您可以使用 transmit 仅向当前订阅发送消息。它并没有真正记录在案,并且不知何故是一种反模式(因为在 pub sub 场景中,频道中的每条消息都应该到达每个用户),但它工作正常。当客户端发送无效请求时,我们将它用于 "responding" 和错误消息。