2017-05-19 1 views
2

Smart Admin theme 인 간단한 레일 앱을 실행 중입니다. 많이 시도했지만 응용 프로그램에서 Action 케이블을 실행할 수 없습니다.ActionCable이 채널에서만 #subscribe를 호출하지 않습니다. 연결 등록

my_own_channel.rb

class MyOwnChannel < ApplicationCable::Channel 
    def subscribed 
    stream_for 'my_own_channel' 
    end 
    def unsubscribed; end 
end 

channel.rb

module ApplicationCable 
    class Channel < ActionCable::Channel::Base 
    end 
end 

connection.rb

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

    def connect 
     self.current_user = find_verified_user 
     logger.add_tags 'ActionCable', current_user.email 
    end 

    protected 

    def find_verified_user 
     if verified_user = env['warden'].user 
     verified_user 
     else 
     reject_unauthorized_connection 
     end 
    end 
    end 
end 

../app/assets/javascripts/channels/my_own.js

App.status_monitor = App.cable.subscriptions.create("MyOwnChannel", { 
    received: function(data) { 
    alert('Received....'); 
    }, 
    connected: function() { 
    alert('connected'); 
    }, 
    disconnected: function() { 
    alert('disconnected now'); 
    } 
}); 

../app/assets/javascripts/cable.js

//= require action_cable 
//= require_self 
//= require_tree ./channels 

(function() { 
    this.App || (this.App = {}); 

    App.cable = ActionCable.createConsumer(); 

}).call(this); 

이 모든 일을 한 후, 로그에서 나는 스트림을 볼 수 없습니다. 로그에 있음 :

Started GET "/cable" for 127.0.0.1 at 2017-05-19 22:20:36 +0630 
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-05-19 22:20:36 +0630 
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) 
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
[ActionCable] [[email protected]] Registered connection (abcdefghijklmnopqrstuvwxyz) 

스트림이 연결되어 있지 않습니다. 다른 문제는 connection.rb의 connect 메소드가 호출되지만 MyOwnChannel의 subscribe 메소드가 호출되지 않는다는 것입니다. 나는 실종 된 것이 없다는 것을 알고 있으며 routes.rb 파일에 /cable 라우트도 마운트했습니다.

+0

이것을 알아 냈습니까? 그렇지 않다면 브라우저의 JS 콘솔에'App.status_monitor'를 입력하면 어떻게됩니까? –

답변

1

알아 냈습니다. 실제 문제는 Pace.js 입니다. 간단히 말해서 액션 케이블이 작동하기 시작했습니다. 2 js 파일 간의 실제 충돌이 무엇인지 알지 못했습니다. 하지만 지금은 일했습니다.

+0

정말로 감사합니다. 하루 종일 문제를 해결 한 후에 도움이되었습니다. –

0

이것은 또한 저에게 중요한 문제였습니다. pace.js를 제거하면 문제를 해결할 것이다, 그러나 당신은 또한 또한 트릭을 할해야 WebSocket을 추적을 해제 할 수 있습니다 : https://github.com/HubSpot/pace/issues/411

:
// set this before you load pace.js 
window.paceOptions = { 
    ajax: { 
    trackWebSockets: false 
    } 
} 

도 속도 프로젝트에 열려 문제가있다 (소스 here 참조)

관련 문제