微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 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中的方法“unsubscribed”,但电缆仍然存在,我仍然可以在我的函数“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

解决方法

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

如果你想断开连接,那么就这样做:

App.cable.disconnect()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐