我有两台服务器

on APIs server

在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

on Customer server

在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,...

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