ActionCable:从前端手动关闭连接

时间:2016-10-29 16:02:52

标签: ruby-on-rails ruby-on-rails-5 actioncable

我无法手动关闭套接字。 它仅在我关闭浏览器中的选项卡时退出。

Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-10-29 16:55:49 +0200

但是当我打电话时

App.cable.subscriptions.remove(App.subscription)

App.subscription.unsubscribe()

方法"取消订阅"正在调用CommunityChannel,但电缆仍然存在,我仍然可以在我的函数中打印它" print_socket"

如何手动关闭连接?

module ApplicationCable
  class Connection < ActionCable::Connection::Base

    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      if current_user = User.find_by(id: cookies.signed[:user_id])
        current_user
      else
        reject_unauthorized_connection
      end
    end

  end
end

1 个答案:

答案 0 :(得分:6)

App.cable.subscriptions.remove(App.subscription)会取消订阅“CommunityChannel”,但不会关闭您的连接,

如果您想断开连接,请执行以下操作:

App.cable.disconnect()