0

我目前正在使用 Rails 6 和 Linux mint Cinnamon 20.2,但我的 Rails 应用程序中的 Redis 配置/设置存在问题。我的问题是我的 ActionCable 不能正常工作。

我想构建一个实时聊天应用程序,我创建了一个名为“Room”的频道。我的 room_channel.js 文件如下所示:

import consumer from "./consumer"

consumer.subscriptions.create({ channel: "RoomChannel", room_id: 3 }, {
connected() {
   console.log('Connected successfully!')
},

disconnected() {
   // Called when the subscription has been terminated by the server
},

received(data) {
   console.log(data)
}
});

这是我的 room_channel.rb:

class RoomChannel < ApplicationCable::Channel
def subscribed
   stream_from "room_channel_#{params[:room_id]}"
end

def unsubscribed
   # Any cleanup needed when channel is unsubscribed
end
end

这是我的 messages_controller.rb 文件的重要部分(ActionCable 命令很重要):

def create
   @message = Message.new(message_params)

   @message.user = User.generate

   @message.save

   ActionCable.server.broadcast("room_channel_#{@message.room_id}",{message:"hello"})

end

当我加入房间 3(3 是那个房间的 ID)时,我收到“连接成功”消息,但是当我发送消息时,它不会在控制台中输出“你好”。这意味着它没有成功接收数据,对吧?

我可以用 Redis 做什么才能接收数据?

最后,我将 config/cable.yml 文件的开发部分更改为,但它没有解决任何问题:(。

development:
   adapter: redis
   url: redis://localhost:6379/1

有人可以帮我解决这个问题吗?谢谢你的帮助!

4

0 回答 0