我有两台服务器
:
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'
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,...
我已经做了很多研究,但仍然可以接收来自广播的任何数据,并且连接的回调也永远不会被调用,请帮忙。感谢