0
アクションケーブルを使用して私のRailsチャットアプリケーションで動的に更新する赤い通知サークルを取得しようとしています。問題は、メッセージが送信されるときに、受信機能を複数回トリガーするように見えることがあることです。私はサブスクリプションが正しく定義されていると確信していますが、何かが間違っています。アクション複数のサブスクリプションのケーブル通知
ここでは、チャットIDパラメータを使用してサブスクリプションを作成しています。同じパラメータが
資産/ javascriptの/チャンネル/ chat.js
$(document).on('turbolinks:load', function() {
submitNewMessage();
$.ajax({
type: "GET",
dataType: "json",
url: "/chats",
success: function(data){
$.each(data, function(i, chat){
App['chat' + chat.id] = App.cable.subscriptions.create({
channel: "ChatChannel",
chat_id: chat.id},
{ received: function(data){
# Code to update notifications
}}
});
}
}
});
function submitNewMessage(){
$('#message_text').keydown(function(event) {
if (event.keyCode == 13) {
var text = event.target.value
App['chat' + chat_id].send({text: text})
$('#message_text').val(" ")
return false;
}
});
}
});
そして、私はまた
チャンネル/ chat_channel.rb
のparams chat_idを使用する加入方法で submitNewMessage機能に使用されていますclass ChatChannel < ApplicationCable::Channel
def subscribed
stream_from "chat_#{param['chat_id']}_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def receive(payload)
Message.create(user_id: current_user.id, chat_id: params["chat_id"], text: payload["text"])
end
end
新しいコメントがトリガーされたときにchat.jsで受信した機能が複数回起動される可能性がありますか?