Skip to content

Commit 2d5221e

Browse files
authored
fix: interrupted WebSocket connection not closed by LiveQuery server (parse-community#8012)
1 parent 468e987 commit 2d5221e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spec/ParseWebSocketServer.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('ParseWebSocketServer', function () {
2323
ws.readyState = 0;
2424
ws.OPEN = 0;
2525
ws.ping = jasmine.createSpy('ping');
26+
ws.terminate = () => {};
2627

2728
parseWebSocketServer.onConnection(ws);
2829

@@ -75,6 +76,30 @@ describe('ParseWebSocketServer', function () {
7576
expect(wssError).toBe('Invalid Packet');
7677
});
7778

79+
it('closes interrupted connection', async () => {
80+
const onConnectCallback = jasmine.createSpy('onConnectCallback');
81+
const http = require('http');
82+
const server = http.createServer();
83+
const parseWebSocketServer = new ParseWebSocketServer(server, onConnectCallback, {
84+
websocketTimeout: 5,
85+
}).server;
86+
const ws = new EventEmitter();
87+
ws.readyState = 0;
88+
ws.OPEN = 0;
89+
ws.ping = jasmine.createSpy('ping');
90+
ws.terminate = jasmine.createSpy('terminate');
91+
92+
parseWebSocketServer.onConnection(ws);
93+
94+
// Make sure callback is called
95+
expect(onConnectCallback).toHaveBeenCalled();
96+
await new Promise(resolve => setTimeout(resolve, 10));
97+
expect(ws.ping).toHaveBeenCalled();
98+
await new Promise(resolve => setTimeout(resolve, 10));
99+
expect(ws.terminate).toHaveBeenCalled();
100+
server.close();
101+
});
102+
78103
afterEach(function () {
79104
jasmine.restoreLibrary('ws', 'Server');
80105
});

src/LiveQuery/ParseWebSocketServer.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ export class ParseWebSocketServer {
1414
logger.info('Parse LiveQuery Server started running');
1515
};
1616
wss.onConnection = ws => {
17+
ws.waitingForPong = false;
18+
ws.on('pong', () => {
19+
this.waitingForPong = false;
20+
});
1721
ws.on('error', error => {
1822
logger.error(error.message);
1923
logger.error(inspect(ws, false));
2024
});
2125
onConnect(new ParseWebSocket(ws));
2226
// Send ping to client periodically
2327
const pingIntervalId = setInterval(() => {
24-
if (ws.readyState == ws.OPEN) {
28+
if (!ws.waitingForPong) {
2529
ws.ping();
30+
ws.waitingForPong = true;
2631
} else {
2732
clearInterval(pingIntervalId);
33+
ws.terminate();
2834
}
2935
}, config.websocketTimeout || 10 * 1000);
3036
};

0 commit comments

Comments
 (0)