fix: interrupted WebSocket connection not closed by LiveQuery server (#8012)
This commit is contained in:
@@ -23,6 +23,7 @@ describe('ParseWebSocketServer', function () {
|
|||||||
ws.readyState = 0;
|
ws.readyState = 0;
|
||||||
ws.OPEN = 0;
|
ws.OPEN = 0;
|
||||||
ws.ping = jasmine.createSpy('ping');
|
ws.ping = jasmine.createSpy('ping');
|
||||||
|
ws.terminate = () => {};
|
||||||
|
|
||||||
parseWebSocketServer.onConnection(ws);
|
parseWebSocketServer.onConnection(ws);
|
||||||
|
|
||||||
@@ -75,6 +76,30 @@ describe('ParseWebSocketServer', function () {
|
|||||||
expect(wssError).toBe('Invalid Packet');
|
expect(wssError).toBe('Invalid Packet');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('closes interrupted connection', async () => {
|
||||||
|
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();
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 10));
|
||||||
|
expect(ws.ping).toHaveBeenCalled();
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 10));
|
||||||
|
expect(ws.terminate).toHaveBeenCalled();
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
jasmine.restoreLibrary('ws', 'Server');
|
jasmine.restoreLibrary('ws', 'Server');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ export class ParseWebSocketServer {
|
|||||||
logger.info('Parse LiveQuery Server started running');
|
logger.info('Parse LiveQuery Server started running');
|
||||||
};
|
};
|
||||||
wss.onConnection = ws => {
|
wss.onConnection = ws => {
|
||||||
|
ws.waitingForPong = false;
|
||||||
|
ws.on('pong', () => {
|
||||||
|
this.waitingForPong = false;
|
||||||
|
});
|
||||||
ws.on('error', error => {
|
ws.on('error', error => {
|
||||||
logger.error(error.message);
|
logger.error(error.message);
|
||||||
logger.error(inspect(ws, false));
|
logger.error(inspect(ws, false));
|
||||||
@@ -21,10 +25,12 @@ export class ParseWebSocketServer {
|
|||||||
onConnect(new ParseWebSocket(ws));
|
onConnect(new ParseWebSocket(ws));
|
||||||
// Send ping to client periodically
|
// Send ping to client periodically
|
||||||
const pingIntervalId = setInterval(() => {
|
const pingIntervalId = setInterval(() => {
|
||||||
if (ws.readyState == ws.OPEN) {
|
if (!ws.waitingForPong) {
|
||||||
ws.ping();
|
ws.ping();
|
||||||
|
ws.waitingForPong = true;
|
||||||
} else {
|
} else {
|
||||||
clearInterval(pingIntervalId);
|
clearInterval(pingIntervalId);
|
||||||
|
ws.terminate();
|
||||||
}
|
}
|
||||||
}, config.websocketTimeout || 10 * 1000);
|
}, config.websocketTimeout || 10 * 1000);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user