Rails ActionCable没有收到数据

时间:2017-06-26 10:31:01

标签: ruby-on-rails ruby websocket actioncable

我有两台服务器

  1. API服务器(Rails& ActionCable)
  2. 客户服务器(Rails 5.1& Vue js& actioncable from Yarn
  3. API服务器上的

    app / channels / operation_channel.rb

    中的

    class OperationChannel < ApplicationCable::Channel
        def subscribed
            stream_from 'operations'
        end
    
        def unsubscribed
            # Any cleanup needed when channel is unsubscribed
        end
    end
    

    在config / environments / production.rb

    config.action_cable.disable_request_forgery_protection = true
    

    在config / routes.rb

    mount ActionCable.server => '/cable'
    
    app / models / operation.rb

    class Operation < ApplicationRecord
        [...]
    
        after_update :broadcast
    
        def broadcast
            ActionCable.server.broadcast "operations", operation: self
        end
    
        [...]
    end
    
    客户服务器上的

    在main.js中

    import ActionCable from 'actioncable'
    
    var cable = ActionCable.createConsumer('wss://[APIs server URL]/cable');
    
    var operations = cable.subscriptions.create('OperationChannel', {
        connected: function() {
            console.log("connected");
        },
        disconnected: function() {
            console.log("disconnected");
        },
        received: function(data) {
            console.log('received');
            console.log(data);
        }
    });
    

    我只能在web-socket服务器停止时调用断开连接的回调,并且我可以在日志中看到web-socket信息:

    Started GET "/cable/" [WebSocket] ...
    Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
    .
    .
    .
    [ActionCable] Broadcasting to operations: {:operation=>#<Operation id: 1,...
    

    我已经做了很多研究,但仍然可以接收来自广播的任何数据,并且连接的回调也永远不会被调用,请帮忙。感谢

0 个答案:

没有答案