我目前有一個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; });
});
});
}
]);
我編輯這篇文章,請看到它,如果你想進一步的信息告訴我。基本上我想要如何在廣播消息期間發送** chatroom_id **,以便它將廣播消息到特定的聊天室? –