【问题标题】:ActionCable message saved but broadcast is ignored in after_create_commitActionCable 消息已保存,但在 after_create_commit 中忽略了广播
【发布时间】:2020-08-19 12:52:19
【问题描述】:

我使用此ActionCable demo 作为设置用户消息传递的指南,但我正在对其进行更改,以便用户之间可以有多个私人房间。消息通过speak 方法保留到数据库,就像[ActionCable] [me] MessagesChannel#speak({"body"=>"eee", "conversation_id"=>1}) 一样,然后使用after_create_commit 调用后台工作人员。工人执行,但广播没有广播。是什么阻止了该广播的执行?

2020-05-10T04:31:32.216Z pid=32378 tid=ouzv4m5h2 class=MessageBroadcastWorker jid=e8b23ada55bede39b811e770 INFO: start
2020-05-10T04:31:32.499Z pid=32378 tid=ouzv4m5h2 class=MessageBroadcastWorker jid=e8b23ada55bede39b811e770 elapsed=0.283 INFO: done

messages.coffee

if ($("meta[name='current-user']").length > 0)
  App.messages = App.cable.subscriptions.create "MessagesChannel",
    connected: ->
      console.log 'Connected'

    disconnected: ->
      console.log 'Disconnected'

    received: (data) ->
      console.log 'Received'

    speak: (body, conversation_id) ->
      @perform 'speak', body: body, conversation_id: conversation_id

  $(document).on 'turbolinks:load', ->
    submit_message()
    scroll_bottom()

  submit_message = () ->
    $('#response').on 'keydown', (event) ->
      if event.keyCode is 13
        conversation_id = $("#messages").data("conversation-id")
        App.messages.speak(event.target.value, conversation_id)
        event.target.value = ""
        event.preventDefault()

  scroll_bottom = () ->
    if $('#messages').length > 0
      $('#messages').scrollTop($('#messages')[0].scrollHeight)

messages_channel.rb

class MessagesChannel < ApplicationCable::Channel
  def subscribed
   #i have to change this so that the conversation id is a part of the stream but i'll do that later 
   stream_from "messages_channel"
  end

  def unsubscribed
    stop_all_streams
  end

  def speak(data)
   Message.create! body: data['body'], conversation_id: data['conversation_id'], user_id: current_user.id
  end
end

message.rb

class Message < ApplicationRecord
  after_create_commit { MessageBroadcastWorker.perform_async self.id }
end

message_broadcast_worker.rb

class MessageBroadcastWorker
 include Sidekiq::Worker

 def perform(message_id)
  message = Message.find message_id
  ActionCable.server.broadcast("messages_channel", message: render_message(message))
 end

 private
 def render_message(message)
  ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message, username: message.user.username })
 end
end

视图/消息/index.html.erb

<div id="messages" data-conversation-id="<%= @conversation.id %>">
 <%= render @messages %>
</div>
<%= form_for [@conversation, @message], remote: true do |f| %>
 <%= f.label :type_a_message %>
 <%= f.text_area :body, placeholder: "Say something.", autofocus: true, id:"response" %>            
<% end %>

views/messages/_message.html.erb

<% cache message do %>
 <div class="message">
  <div><strong><%= message.user.username %>:</strong> <%= message.body %></div>
  <div class="date"><%= local_time(message.created_at) %></div>
 </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails coffeescript actioncable


    【解决方案1】:

    我的config/cable.yml 中有两行注释掉:

    development:
     adapter: redis
     url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
    

    重新添加这些内容让我的应用栩栩如生

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 1970-01-01
      • 2022-07-02
      • 2013-09-09
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      相关资源
      最近更新 更多