我在Heroku平台上使用带有Node.js的Socket.IO(最新版本1.3.6),我开发了一个用express.js构建的基本聊天应用程序.对于大多数聊天聊天的客户来说,它非常适用,但是对于一个客户端,我从heroku dyn
heroku router - - at=error code=H12 desc="Request timeout" method=GET path="/socket.io/?EIO=3&transport=polling&t=1442928164852-20&sid=OIjnoxv2RqP3ijBnAAAA" host=xxx.herokuapp.com fwd="x.x.x.x" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 heroku router - - at=error code=H12 desc="Request timeout" method=GET path="/socket.io/?EIO=3&transport=polling&t=1442924841606-16&sid=5yukgKd2YUl75t1rAAAI" host=xxx.herokuapp.com fwd="x.x.x.x" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 ...
他可以毫无问题地发送和接收消息,但是,例如,当他关闭浏览器的选项卡时,不会触发断开事件并且服务器端的连接超时.此客户端位于公司Web代理后面.
这是服务器端代码:
var heartbeatInterval = 50000; socket.init = function( io ) { function sendHeartbeat() { setTimeout( sendHeartbeat, heartbeatInterval ); io.emit('ping', { beat : 1 }); } io.on('connection', function( socket ) { var session = socket.handshake.session; var dateFormat = 'DD/MM à HH:mm:ss' var time = moment().tz('Europe/Paris').format( dateFormat ); if( session.user ) { users.exist(session.user.name, function( exist ) { if( ! exist ) { users.add( session.user ); users.list(function( usersList ) { io.emit('user_new'); addBotMessage(io, time, session.user.name + " connected"); async.eachSeries(usersList, function( user, next ) { io.emit('user_connected', user); next(); }, function() { socket.on('pong', function( data ) { debug('Pong received from client'); }); socket.on('message', function( message ) { time = moment().tz('Europe/Paris').format( dateFormat ); if( message && message.length <= 1000 ) addMessage(io, time, session.user, marked( message )); }); socket.on('disconnect', function() { users.remove( session.user.name ); time = moment().tz('Europe/Paris').format( dateFormat ); io.emit('user_disconnected', session.user.id); addBotMessage(io, time, session.user.name + " disconnected"); }); }); }); } else { io.to( socket.id ).emit('already_connected'); } }); } }); setTimeout( sendHeartbeat, heartbeatInterval ); };
我不知道如何解决这个问题,任何想法/建议?
您需要使用http服务器包装socket.io