我已经从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
    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)
  }
});

在页面加载时弹出警告说已连接。在另一个终端run 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

10-07 18:57
查看更多