ruby-on-rails - 广播消息发送后不会在浏览器控制台显示,actioncable和redis开发环境
问题描述
我已经为从 redis cli 运行服务器的 windows 设置了 redis
C:\program files\redis>redis-cli
127.0.0.1:6379>
开发 cable.yml 是
development:
adapter: redis
url: redis://127.0.0.1:6379/0
通知频道 rb
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "notifications_#{current_user.id}"
end
end
启动 Rails 服务器并加载页面,服务器打印
Started GET "/cable/" [WebSocket] for ::1 at 2018-09-29 13:07:17 +1000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Registered connection (Z2lkOi8vcGVvcGxlcy9Vc2VyLzUwMQ)
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notifications_501
页面上的通知.js 是
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
connected: function() {
alert("hello")
},
recieved: function(data) {
console.log(data)
}
});
弹出警报说页面加载时已连接。在另一个终端运行 rails 控制台
irb> ActionCable.server.broadcast("notifications_501", body: "hello")
回顾 Rails 服务器输出
NotificationsChannel transmitting {"body"=>"hello"} (via streamed from notifications_501)
但是控制台没有显示 JSON 对象?
**编辑**
创建另一个redis服务器实例
C:\program files\redis>redi-cli
127.0.0.1:6379>subscribe "notifications_501"
1)"subscribe"
2)"notifications_501"
3)(integer) 1
打开另一个cli
127.0.0.1:6379>publish "notifications_502" "{\"body\":\"hello\"}"
它同时传输到 rails 服务器和订阅的 redis 服务器
NotificationsChannel transmitting {"body"=>"hello"} (via streamed from notifications_501)
解决方案
我认为它在您第一次尝试时对您不起作用的原因可能是因为命名received
函数时的拼写错误。recieved
相反,你做到了。
推荐阅读
- python - Python 以编程方式更改控制台字体大小
- java - 如何在 Java 中将文本添加到游戏中
- php - 不调用翻译 .po 文件
- java - 如何在java中反序列化和序列化zonedDateTime
- spring-data-jpa - Spring Data JPA:通过示例在查询中具有必填字段和可选字段
- mysql - 不能使用内部查询
- python - 自动刷新 tkinter 标签而不是按下按钮
- apache-kafka - 为什么通过主题 1 到达主题 2 的消息在 Apache Kafka 中具有比主题 1 更早的代理时间戳?
- visual-studio-code - VS Code 缺少全局任务菜单
- c# - AJAX 没有被触发,asp.net