【发布时间】:2023-01-06 21:24:28
【问题描述】:
我正在使用 Windows 操作系统在 Rails 6 中开发实时聊天应用程序,但我的 ActionCable 有问题。
开发适配器根本不起作用(我猜),既不是异步的,也不是 Redis 的。我尝试了一切,但我真的被困在这一点上:(。
我有一个名为“room”的频道,其后端有以下编码 (app/channels/room_channel.rb):
class RoomChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
reject unless params[:room_id]
room = Room.find params[:room_id].to_i
stream_for room
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
在其前端:(app/javascript/room_channel.js):
import consumer from "./consumer"
let url = window.location.href;
let room_id = parseInt(url.substring(url.search("rooms/") + 6) );
if (url.indexOf("rooms/") != -1) {
console.log('Subscribed to room', room_id);
consumer.subscriptions.create({ "channel": "RoomChannel", "room_id": room_id }, {
connected() {
console.log('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) {
console.log('data received successfully')
// Called when there's incoming data on the websocket for this channel
}
});
}
当我运行服务器时,我可以订阅并连接到该频道,但该频道无法接收任何传入数据(在我的例子中是消息)。我知道它是因为当我在房间中创建新消息时它不会输出控制台消息(“数据已成功接收”)。
其他重要信息是当我的同事在他的计算机上以相同的编码运行这个应用程序时,他可以接收数据(当他向房间发送消息时,他得到“数据接收成功”输出)。正如我所说,我们到处都有完全相同的编码!
所以我确定这不是代码的错,问题出在我的电脑上,或者我不知道。
有人可以帮我解决这个问题吗?感谢阅读和等待热心人士的解答! :)
【问题讨论】:
-
检查您的连接是否由于某种原因被断开。在
room_channel.js文件中的disconnected方法中做控制台登录
标签: ruby-on-rails ruby-on-rails-6 actioncable