发表于 2016年09月27日 15时12分52秒
修改之前:
全站页面都挂载了actioncable的js
这导致不管什么页面都会打开websocket。很显然不喜欢这样。
把它从application.js中删除
在room页面增加
<%=javascript_include_tag 'cable', 'data-turbolinks-eval': false%>
assets.rb中增加
Rails.application.config.assets.precompile += %w( cable.js )
修改cable.js为
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the rails generate channel command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function () {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
$(document).on("turbolinks:load", function () {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
});
room.coffee添加关闭websocket的代码
$(document).on 'turbolinks:before-cache', ->
App.cable.connection.close()