Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interrupted WebSocket connection not closed by LiveQuery server #8012

Merged
Show file tree
Hide file tree
Changes from 6 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('closes interrupted 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 () {
pash2048 marked this conversation as resolved.
Show resolved Hide resolved
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;
pash2048 marked this conversation as resolved.
Show resolved Hide resolved
} else {
clearInterval(pingIntervalId);
ws.terminate();
}
}, config.websocketTimeout || 10 * 1000);
};
Expand Down