2016-10-02 42 views
4

有沒有辦法來終止訂閱到特定頻道從服務器端(控制器)任何特定的消費,這樣在我的咖啡腳本文件斷開的回調可以調用?如何終止從服務器對actioncable頻道的訂閱?

+0

我很好奇這一點。我決定向客戶端發送「斷開連接」消息,客戶端在收到該消息後進行訂閱終止。 –

回答

0

你可以做這樣的事情。

class YourChannel < ApplicationCable::Channel 

    #your code 

    def your_custom_action 
    if something 
     reject_subscription 
    end 
    end 
end 
0

http://api.rubyonrails.org/classes/ActionCable/Channel/Base.html#class-ActionCable::Channel::Base-label-Rejecting+subscription+requests

class ChatChannel < ApplicationCable::Channel 
    def subscribed 
    @room = Chat::Room[params[:room_number]] 
    reject unless current_user.can_access?(@room) 
    end 
end 

在致電reject你也可以告知拒絕的理由是用戶:

class ChatChannel < ApplicationCable::Channel 
    def subscribed 

    if params["answerer"] 

     answerer = params["answerer"] 

     answerer_user = User.find_by email: answerer 

     if answerer_user 

     stream_from "chat_#{answerer_user}_channel"  

     else 

     connection.transmit identifier: params, error: "The user #{answerer} not found." 

    # http://api.rubyonrails.org/classes/ActionCable/Channel/Base.html#class-ActionCable::Channel::Base-label-Rejecting+subscription+requests 

     reject 

     end 

    else 

     connection.transmit identifier: params, error: "No params specified." 

    # http://api.rubyonrails.org/classes/ActionCable/Channel/Base.html#class-ActionCable::Channel::Base-label-Rejecting+subscription+requests 

     reject 

    end  

    end 
end