【发布时间】:2017-06-18 03:57:34
【问题描述】:
我目前有一个 Rails 5 应用程序作为我的后端,我们可以称之为“核心”。我还有另一个 Angular 1.6.4 应用程序作为我的前端,它为 Angular 客户端提供服务,并通过 angular-actionable 与后端应用程序集成,我们可以称之为“前端”。这是两个完全不同的应用程序,具有完全不同的域。
基本上,我正在尝试通过 Core 集成 Action Cable 并让它与 Front 对话。我在这里为 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
堡垒
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 **,以便它将消息广播到特定的聊天室????
标签: angularjs websocket chat ruby-on-rails-5 actioncable