问题描述
首先,ActionCable功能非常新。我在这里一直在关注该教程:https://www.driftingruby.com/episodes/push-notifications-with-actioncable
创建了一个名为web_notifications_channel
和web_notifications_channel.rb的频道:
class WebNotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "web_notifications_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
web_notifications_channel.js:
import consumer from "./consumer"
consumer.subscriptions.create("WebNotificationsChannel",{
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) {
// Called when there's incoming data on the websocket for this channel
console.log(`wtf`)
}
});
控制台中没有任何显示。
已将适配器设置更改为cable.yml中的redis,以便从控制台请求:
development:
adapter: redis
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REdis_URL") { "redis://localhost:6379/1" } %>
channel_prefix: community_rails_production
运行控制台命令:
ActionCable.server.broadcast 'web_notifications_channel','You have visited the welcome page.'
我得到的答复是:
broadcasting to web_notifications_channel: "You have visited the welcome page."
=> 0
但是我的received
方法从未被调用!我在这里疯了,因为无法理解发生了什么!我检查了一些类似的问题,但没有一个对我有帮助。
PS:我已经添加了redis gem,并从控制台开始了redis。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)