广播消息发送后不会在浏览器控制台显示,actioncable和redis开发环境
Broadcasted message will not show up in browser console after being sent, actioncable and redis development env
我已经为 windows 运行从 redis cli
连接服务器设置了 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
notifications.js 页面上的
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
connected: function() {
alert("hello")
},
recieved: function(data) {
console.log(data)
}
});
页面加载时弹出警告提示已连接。在另一个终端 运行 rails console
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)
将 notifications.js
更改为 notfications.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 不起作用..
我认为它在您第一次尝试时不起作用的原因可能是命名 received
函数时出现错字。您改为 recieved
。
我已经为 windows 运行从 redis cli
连接服务器设置了 redisC:\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
notifications.js 页面上的
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
connected: function() {
alert("hello")
},
recieved: function(data) {
console.log(data)
}
});
页面加载时弹出警告提示已连接。在另一个终端 运行 rails console
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)
将 notifications.js
更改为 notfications.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 不起作用..
我认为它在您第一次尝试时不起作用的原因可能是命名 received
函数时出现错字。您改为 recieved
。