我不知道如何摆脱Idle connection
错误。它曾经更糟糕,但它仍然每小时左右发生一次。这是Papertrail得到的日志:
Aug 09 23:07:25 heroku/router: at=info method=GET path="/a/login/" host=... request_id=f99431f8-9ce9-4e1e-b16f-0ae2b50870ab fwd="74.210.150.28" dyno=web.1 connect=1ms service=258ms status=304 bytes=305
Aug 09 23:07:26 heroku/router: at=info method=GET path="/js/auth.js" host=app.sofiatutors.com request_id=12896c98-9803-4161-bf5b-06028123ff86 fwd="74.210.150.28" dyno=web.1 connect=2ms service=3ms status=304 bytes=240
Aug 09 23:07:56 heroku/router: at=info method=POST path="/api/email/reset/qEUsZwkRvp750OBgCFuXImiCWPhQbTD0vFVaGEyCZJJXEJGpm6Gi7cgKWsLqHOTusRHieWyVgEbqe6T5qH7wCJT9ROToeHnINhTDvq5unWT6r7fwh4ha59PlQVwB6TmS" host=app.sofiatutors.com request_id=ae44cd3d-cb06-42bd-8948-9e8ca8273ebe fwd="74.210.150.28" dyno=web.1 connect=1ms service=395ms status=200 bytes=426
Aug 09 23:08:11 heroku/router: at=info method=POST path="/api/email/reset/qEUsZwkRvp750OBgCFuXImiCWPhQbTD0vFVaGEyCZJJXEJGpm6Gi7cgKWsLqHOTusRHieWyVgEbqe6T5qH7wCJT9ROToeHnINhTDvq5unWT6r7fwh4ha59PlQVwB6TmS" host=app.sofiatutors.com request_id=a0ed9edc-a561-4518-a61c-3d22fa9445aa fwd="74.210.150.28" dyno=web.1 connect=0ms service=214ms status=200 bytes=426
Aug 09 23:08:21 heroku/router: at=error code=H15 desc="Idle connection" method=GET path="/socket.io/?EIO=3&transport=websocket" host=app.sofiatutors.com request_id=a5db86f2-7561-44db-bc1d-22ae69ac3a30 fwd="74.210.150.28" dyno=web.1 connect=1ms service=56301ms status=503 bytes=279
一开始,我认为这是因为用户正在上传文件或类似的内容,但事实并非如此。从我看到的,用户只是重置他的密码。更奇怪的是,除了乒乓球之外,目前还没有使用WebSocket!
我在网上找不到任何解决方案。我正在为我的DNS使用CNAME记录。我通过将ping
发送到我的服务器来保持连接处于活动状态,该服务器每20秒使用我的WebSocket以pong
回答。我在Socket.io中强制连接到WebSocket(我通过XHR轮询获得了更多空闲连接)。
这是我的客户端代码:
const socket_url = environment.indexOf('dev') === -1 ? "wss://" + window.location.host : 'ws://localhost:3000';
var socket = io.connect(socket_url, { transports : ["websocket"] });
let socketIoMiddleware = createSocketIoMiddleware(socket, "server/");
socket.on('connect', function() {
console.log("L'utilisateur s'est connecté à un Socket.");
// Init code
});
socket.on('disconnect', function(){
console.log("Déconnexion au socket.")
});
socket.emit('ping');
socket.on('pong', function() {
console.log("pong", new Date().toString());
var to = setTimeout(function(){
console.log("ping", new Date().toString());
socket.emit('ping');
clearTimeout(to)
},20000);
});
我的服务器代码(Node.JS with Express):
io.sockets.on('connection', function (socket) {
socket.on('join-auth', function (data) {
socket.join(data.session); // We are using room of socket io
var room = data.session;
socket.on('ping', function(){
// console.log("ping pong", new Date().toString())
io.sockets.in(room).emit('pong');
})
// The rest of my socket
我知道Heroku对WebSockets有55秒超时但我根本不明白为什么连接空闲或如何修复它。有没有人有想法?