1

Мне нужно воспроизвести аудио файл при получении сообщения у получателя.

class MessageBroadcastJob < ApplicationJob
  queue_as :default

  def perform(message)
    sender = message.profile
    recipient = message.conversation.opposed_profile(sender)

    broadcast_to_sender(sender, message)
    broadcast_to_recipient(recipient, message)
  end

  private

  def broadcast_to_sender(profile, message)
    ActionCable.server.broadcast(
      "conversations-#{profile.id}",
      message: render_message(message, profile),
      conversation_id: message.conversation_id
    )
  end

  def broadcast_to_recipient(profile, message)
    # Здесь нужно сделать воспроизведение аудио файла
    ActionCable.server.broadcast(
      "conversations-#{profile.id}",
      window: render_window(message.conversation, profile),
      message: render_message(message, profile),
      conversation_id: message.conversation_id
    )
  end

  def render_message(message, profile)
    ApplicationController.render(
      partial: 'messages/message',
      locals: { message: message, profile: profile }
    )
  end

  def render_window(conversation, profile)
    ApplicationController.render(
      partial: 'conversations/conversation',
      locals: { conversation: conversation, profile: profile }
    )
  end
end

1 ответ 1

1

Я решил данный вопрос другим способом

var conversation = $('#conversations-list').find("[data-conversation-id='" + data['conversation_id'] + "']");

if (data['window'] !== undefined) {
  $('#messageAudio').trigger("play");
  conversation.find('.messages-list').find('ul').append(data['message']);
}
else {
  conversation.find('ul').append(data['message']);
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.