Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/ParseWebSocketServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('ParseWebSocketServer', function () {
ws.readyState = 0;
ws.OPEN = 0;
ws.ping = jasmine.createSpy('ping');
ws.terminate = () => {};

parseWebSocketServer.onConnection(ws);

Expand Down Expand Up @@ -75,6 +76,35 @@ describe('ParseWebSocketServer', function () {
expect(wssError).toBe('Invalid Packet');
});

it('can handle broken connection', function (done) {
const onConnectCallback = jasmine.createSpy('onConnectCallback');
const http = require('http');
const server = http.createServer();
const parseWebSocketServer = new ParseWebSocketServer(server, onConnectCallback, {
websocketTimeout: 5,
}).server;
const ws = new EventEmitter();
ws.readyState = 0;
ws.OPEN = 0;
ws.ping = jasmine.createSpy('ping');
ws.terminate = jasmine.createSpy('terminate');

parseWebSocketServer.onConnection(ws);

// Make sure callback is called
expect(onConnectCallback).toHaveBeenCalled();
// Make sure we ping to the client
setTimeout(function () {
expect(ws.ping).toHaveBeenCalled();
setTimeout(function () {
//make sure we close the connection after timeout cause we don't answer the ping
expect(ws.terminate).toHaveBeenCalled();
server.close();
done();
}, 10);
}, 10);
});

afterEach(function () {
jasmine.restoreLibrary('ws', 'Server');
});
Expand Down
8 changes: 7 additions & 1 deletion src/LiveQuery/ParseWebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ export class ParseWebSocketServer {
logger.info('Parse LiveQuery Server started running');
};
wss.onConnection = ws => {
ws.isAlive = true;
ws.on('pong', () => {
this.isAlive = true;
});
ws.on('error', error => {
logger.error(error.message);
logger.error(inspect(ws, false));
});
onConnect(new ParseWebSocket(ws));
// Send ping to client periodically
const pingIntervalId = setInterval(() => {
if (ws.readyState == ws.OPEN) {
if (ws.isAlive) {
ws.ping();
ws.isAlive = false;
} else {
clearInterval(pingIntervalId);
ws.terminate();
}
}, config.websocketTimeout || 10 * 1000);
};
Expand Down