ruby-on-rails - 如何修复 ActionCable 不附加数据
问题描述
我正在为我在 Rails 中的聊天室频道设置 ActionCable。
问题是每次登录用户发送消息时,比如 UserA,消息都不会附加到聊天室。我曾尝试同时登录另一个用户,比如 UserB,来自 UserB 的消息被附加到 UserA 的聊天室,但不是 userB,反之亦然。
/app/channels/chatroom_channel.rb
class ChatroomChannel < ApplicationCable::Channel
def subscribed
stream_from "chatroom_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
/app/assets/javascripts/channels/chatroom.coffee
App.chatroom = App.cable.subscriptions.create "ChatroomChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
alert data
$('#messages').append(data)
$('#message_content').val = ''
scrollToBottom()
return
scrollToBottom = ->
if $('#messages').length > 0
last_message = $('#messages')[0]
last_message.scrollTop = last_message.scrollHeight - (last_message.clientHeight)
$(document).ready ->
scrollToBottom()
return
jQuery(document).on 'turbolinks:load', ->
scrollToBottom()
return
# Called when there's incoming data on the websocket for this channel
这是我的聊天室视图的一部分:/app/views/chatrooms/index.html.erb
<div class="twelve wide column">
<div class="ui fluid raised card chatbox">
<div class="content">
<div class="ui feed" id="messages">
<%= render @messages %>
</div>
</div>
<div class="extra content">
<%= form_for(@message, html: {class: "ui reply form", role: "form"}, url: message_path) do |f| %>
<div class="field">
<div class="ui fluid icon input">
<%= f.text_field :body, id: "message_content"%>
<%= f.button '<i class="bordered inverted orange edit icon"></i>'.html_safe %>
</div>
</div>
<% end %>
</div>
</div>
</div>
我已将开发从更改async
为redis
in,config/cable.yml
但仍然没有成功。感谢您提前阅读
解决方案
推荐阅读
- c - 有人可以帮我解释我的 c 代码有什么问题吗?
- tensorflow - Keras 使用经过训练的 InceptionV3 模型 + CIFAR10 得到关于 Batch Size 的错误
- r - R 和 dplyr:如何将 ifelse 条件与外部数据框一起使用
- prolog - 迭代到 Prolog 中列表中的列表
- .net - EWS + MSAL 并存储 OAuth 令牌以供以后使用
- swift - 如何像stackOverFlow一样增加/减少UpVote/DownVote?
- c++ - 在恒定时间内修剪 C++ 字符串
- raku - perl6/raku:如何限制变量中允许的值?
- qt - 你如何在 QMenuBar 中放置一个组框?
- php - 有没有一种简短的方法来检查值的独特性而不使用 PHP 中的“if”和多个“and”?