Improve WebSocketServer Error Handling (#6230)

* Improve WebSocketServer Error Handling

Closes: https://github.com/parse-community/parse-server/issues/6173

Prevents an unhandled server rejection.

Includes an example for LiveQuery test and closing the proper connections.

Improve live query monitoring

* fix tests
This commit is contained in:
Diamond Lewis
2019-11-22 15:23:04 -06:00
committed by GitHub
parent dff682567d
commit 5bf87d86bb
6 changed files with 90 additions and 0 deletions

View File

@@ -13,10 +13,12 @@ export class WSAdapter extends WSSAdapter {
onListen() {}
onConnection(ws) {}
onError(error) {}
start() {
const wss = new WebSocketServer({ server: this.options.server });
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
wss.on('error', this.onError);
}
close() {}
}

View File

@@ -4,6 +4,7 @@
// Adapter classes must implement the following functions:
// * onListen()
// * onConnection(ws)
// * onError(error)
// * start()
// * close()
//
@@ -22,6 +23,7 @@ export class WSSAdapter {
constructor(options) {
this.onListen = () => {};
this.onConnection = () => {};
this.onError = () => {};
}
// /**
@@ -36,6 +38,13 @@ export class WSSAdapter {
// */
// onConnection(ws) {}
// /**
// * Emitted when error event is called.
// *
// * @param {Error} error - WebSocketServer error
// */
// onError(error) {}
/**
* Initialize Connection.
*

View File

@@ -392,6 +392,8 @@ class ParseLiveQueryServer {
event: 'ws_disconnect',
clients: this.clients.size,
subscriptions: this.subscriptions.size,
useMasterKey: client.hasMasterKey,
installationId: client.installationId,
});
});

View File

@@ -23,6 +23,9 @@ export class ParseWebSocketServer {
}
}, config.websocketTimeout || 10 * 1000);
};
wss.onError = error => {
logger.error(error);
};
wss.start();
this.server = wss;
}