ruby-on-rails - ActionCable 消息已保存,但在 after_create_commit 中忽略了广播
问题描述
我使用此ActionCable 演示作为设置用户消息传递的指南,但我正在对其进行更改,以便用户之间可以有多个私人房间。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
信息.咖啡
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
消息.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 %>
视图/消息/_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 %>
解决方案
我在我的注释中有两行config/cable.yml
:
development:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
重新添加这些使我的应用程序栩栩如生
推荐阅读
- php - Cloudflare API - 使用 CURL PHP 创建 DNS (NS) 记录
- c# - XML 格式生成问题
- android - webview使用zxing从二维码接收链接
- c# - 非静态类和垃圾回收中引用类型的静态成员
- ios - 不要使用 Kingfisher 夸大响应图像
- amazon-web-services - aws sns 短信使用报告,能够保存消息正文吗?
- functional-programming - F# 中泛型类型的泛型函数
- here-api - Here.com api,getMapPackageAtGeoCoordinates 获取多个坐标
- typescript - 在 App.vue 中注册 NavBar 组件时遇到问题
- javascript - 使用 Meetups API javascript 获取数据