2017-06-18 66 views
2

我目前有一個Rails 5應用程序作爲我的後端,我們可以稱之爲「Core」。我還有另一個作爲我的前端的Angular 1.6.4應用程序,它服務於Angular客戶端,並通過角度可操作的方式與後端應用程序集成,我們可以稱之爲「Front」。這是兩個完全獨立的應用程序,具有完全不同的域Action cable:如何在Rails 5中只播放特定聊天室的聊天消息?

基本上,我正在嘗試通過Core集成Action Cable,並將其與Front進行對話。我在這裏使用這個服務的前端:enter link description here。就核心而言,這只是基本的動作電纜設置。

我有一個管理端的聊天室列表。

問題:我從客戶端發送消息,但是它向管理端的所有聊天室廣播消息。我試圖給出聊天室在流中的具體路徑,但它仍然向所有聊天室廣播消息。

我要廣播的消息,具體聊天室

核心

chat_channel.rb

class ChatChannel < ApplicationCable::Channel 
    def subscribed 
     stream_from stream_name 
    end 
    def unsubscribed 
    stop_all_streams 
    end 

    def receive(data) 
     ActionCable.server.broadcast stream_name, data.fetch('message') 
    end 

    private 

    def stream_name 
     "chat_channel_#{chat_id}" 
    end 

    def chat_id 
     params.fetch('data').fetch('chat') 
    end 
end 

Fornt

chatCtrl.js

app.run(function (ActionCableConfig){ 
    ActionCableConfig.debug = true; 
    ActionCableConfig.wsUri= "ws://localhost:3000/cable"; 
}); 
app.controller('chatCtrl', ['$scope', 'ActionCableChannel', 
function($scope, ActionCableChannel) { 

    var consumer = new ActionCableChannel("ChatChannel", { chat: 'localhost:3000/#!/chat/1'}); 
    var callback = function(message) { 
    $scope.myData.push(message); 
    }; 
    consumer.subscribe(callback).then(function(){ 
    $scope.sendToMyChannel = function(message){ 
     consumer.send(message); 
    }; 
    $scope.$on("$destroy", function(){ 
     consumer.unsubscribe().then(function(){ $scope.sendToMyChannel = undefined; }); 
    }); 
    }); 
} 
]); 
+0

我編輯這篇文章,請看到它,如果你想進一步的信息告訴我。基本上我想要如何在廣播消息期間發送** chatroom_id **,以便它將廣播消息到特定的聊天室? –

回答

0

這是我的問題的解決方案,你的差不多,但我沒有用cofeescript,只是JavaScript的..所以我也附上我的代碼,如果你需要它。我只是檢查從DOM屬性data-chat-room-id等於一個我與消息傳遞:

消息控制器創建消息,然後用這些參數ActionCable.server.broadcast電話(信息,用戶,chatroom_id,lastuser)我的js代碼in messages.js

class MessagesController < ApplicationController 
    def create 
    message = Message.new(message_params) 
    message.user = current_user 
    chatroom = message.chatroom 
    if message.save 
     ActionCable.server.broadcast 'messages', 
     message: message.content, 
     user: message.user.name, 
     chatroom_id: message.chatroom_id, 
     lastuser: chatroom.messages.last(2)[0].user.name 
     head :ok 
    end 
    end 
end 

裏面app/assets/javascrips/channels/messages.js我做了檢查data.chatroom_id(從DOM元素)等於

App.messages = App.cable.subscriptions.create('MessagesChannel', {  
    received: function(data) { 
    messages = $('#chatroom_id'); 
    chat_room_id = messages.data('chat-room-id'); 
    if (data.chatroom_id == chat_room_id) { 
     $('#messages').append(this.renderMessage(data)); 
     }; 
    }, 
    renderMessage: function(data) { 
     return "<br><p> <strong>" + data.user + ": </strong>" + data.message + "</p>"; 
    } 
}); 

這是給我的文章(剛纔保存的對象消息從控制器發送的元素)chat_room_id這個答案

Action Cable chat Ilya Bodrov-Krukowski

伊利亞·博德羅夫-Krukowski上Github

要解決的另一個問題是提供我們的腳本與房間的ID。讓我們藉助HTML數據來解決它 - 屬性:

views/chat_rooms/show.html。ERB

<div id="messages" data-chat-room-id="<%= @chat_room.id %>"> 
    <%= render @chat_room.messages %> 
</div> 

有了這個的地方,我們可以利用房間的ID在腳本:

的JavaScript /渠道/ rooms.coffee

jQuery(document).on 'turbolinks:load', -> 
    messages = $('#messages') 
    if $('#messages').length > 0 

    App.global_chat = App.cable.subscriptions.create { 
     channel: "ChatRoomsChannel" 
     chat_room_id: messages.data('chat-room-id') 
     }, 
     connected: -> 
     # Called when the subscription is ready for use on the server 

     disconnected: -> 
     # Called when the subscription has been terminated by the server 

     received: (data) -> 
     # Data received 

     send_message: (message, chat_room_id) -> 
     @perform 'send_message', message: message, chat_room_id: chat_room_id