Skip to content

Commit

Permalink
socket: reset ping timeout on incoming data
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Oct 16, 2012
1 parent f8bc8c8 commit be7b4e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ Socket.prototype.onOpen = function () {

Socket.prototype.onPacket = function (packet) {
if ('open' == this.readyState) {
// Reset ping timeout on any packet, incoming data is a good sign of
// other side's liveness
this.setPingTimeout();

switch (packet.type) {

case 'ping':
debug('got ping');
this.sendPacket('pong');
this.emit('heartbeat');
this.setPingTimeout();
break;

case 'error':
Expand Down
14 changes: 9 additions & 5 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

var http = require('http')
, parser = eio.parser
, WebSocket = require('ws')
, fs = require('fs');
, WebSocket = require('ws');

/**
* Tests.
Expand Down Expand Up @@ -725,10 +724,15 @@ describe('server', function () {
});

it('should interleave with pongs if many messages buffered after connection open', function (done) {
var opts = { transports: ['websocket'], pingInterval: 10, pingTimeout: 5 };
var opts = {
transports: ['websocket'],
pingInterval: 200,
pingTimeout: 100
};

var engine = listen(opts, function (port) {
var messageCount = 50;
var messagePayload = fs.readFileSync(__filename);
var messageCount = 100;
var messagePayload = new Array(1024 * 1024 * 1).join('a');
var connection = null;
engine.on('connection', function (conn) {
connection = conn;
Expand Down

0 comments on commit be7b4e7

Please sign in to comment.