Ruby on rails 发送后,广播消息将不会显示在浏览器控制台中,actioncable和redis development env
我已经在redis cli中为运行服务器的windows安装了redisRuby on rails 发送后,广播消息将不会显示在浏览器控制台中,actioncable和redis development env,ruby-on-rails,redis,actioncable,Ruby On Rails,Redis,Actioncable,我已经在redis cli中为运行服务器的windows安装了redis C:\program files\redis>redis-cli 127.0.0.1:6379> 开发电缆.yml是 development: adapter: redis url: redis://127.0.0.1:6379/0 通知频道rb class NotificationsChannel < ApplicationCable::Channel def subscribed
C:\program files\redis>redis-cli
127.0.0.1:6379>
开发电缆.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
第页上的notifications.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 server的另一个实例
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)
将
notifications.js
更改为notifications.coffee
并使用如下代码
App.notificationss = App.cable.subscriptions.create "NotificationsChannel",
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("hello")
# Called when there's incoming data on the websocket for this channel
不确定普通javascript为什么不起作用。我认为,在您第一次尝试时,它不起作用的原因可能是命名
接收的函数时输入了一个错误。您确实收到了
。有人有什么想法吗?我已经开始工作了,但不得不将我的notifications.js改为coffee。不知道为什么你肯定是对的!我当时试过了,效果很好。它还帮助我解决了另一个问题,同时尝试使其他DOM行为正常工作,但我无法在接收函数中使用coffeescript。。谢谢
App.notificationss = App.cable.subscriptions.create "NotificationsChannel",
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("hello")
# Called when there's incoming data on the websocket for this channel