我目前有一个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 **,以便它将广播消息到特定的聊天室? –