代码之家  ›  专栏  ›  技术社区  ›  Lee Eather

广播消息在发送后不会显示在浏览器控制台中,actioncable和redis development env

  •  0
  • Lee Eather  · 技术社区  · 6 年前

    我已经在redis cli上为windows安装了redis服务器

    C:\program files\redis>redis-cli
    127.0.0.1:6379>
    

    开发cable.yml is

    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
    

    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")
    

    NotificationsChannel transmitting {"body"=>"hello"} (via streamed from notifications_501)
    

    **

    创建redis server的另一个实例

    C:\program files\redis>redi-cli
    127.0.0.1:6379>subscribe "notifications_501"
    1)"subscribe"
    2)"notifications_501"
    3)(integer) 1
    

    127.0.0.1:6379>publish "notifications_502" "{\"body\":\"hello\"}"
    

    它传输到rails服务器和订阅的redis服务器

    2 回复  |  直到 6 年前
        1
  •  1
  •   John Baker    6 年前

    received 功能。你做到了 recieved 相反。

        2
  •  0
  •   Lee Eather    6 年前

    改变 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不起作用。。