在开发和生产环境中,我注意到 websocket 连接每 3 分钟关闭一次(似乎每次正好在 2.95 到 2.98 分钟之间)。
频道:
class SubscriptionIntentionsSelectionChannel < ApplicationCable::Channel
def subscribed
Rails.logger.info("subscribed")
stream_for Project.find(6)
end
def receive(_data)
self.class.broadcast_to(Project.find(6), status: :success, message: "pong")
end
end
客户:
$('document').ready(() => {
channel = App.cable.subscriptions.create(
{ channel: 'SubscriptionIntentionsSelectionChannel' }, {
received(data) {
console.log(data);
},
rejected() {
},
disconnected() {
}
});
setInterval(() => {
channel.send({ data: 'ping' });
}, 10000);
});
我尝试实现一个
setInterval
客户端来向频道发送消息以防止任何不活动,但它没有帮助。无论活动和来回发送的消息数量如何,连接总是在 3 分钟后终止。
连接正常终止。这是来自服务器的日志:
SubscriptionIntentionsSelectionChannel is transmitting the subscription confirmation
SubscriptionIntentionsSelectionChannel is streaming from subscription_intentions_selection:Z2lkOi8vYmxhc3QvUHJvamVjdC82
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2023-05-04 11:22:12 +0200
SubscriptionIntentionsSelectionChannel stopped streaming from subscription_intentions_selection:Z2lkOi8vYmxhc3QvUHJvamVjdC82
这是浏览器中开发人员控制台的日志:
我怀疑 Redis 或 Puma 正在关闭连接。将 cable.yml 适配器从
redis
切换到 async
并没有解决问题。从 puma 开始,config/puma.rb 中的 worker_timeout
参数也没有帮助......也许有办法获得有关连接如何/为什么关闭的更多信息?