Fix wss connection initialisation race

Summary:
The websocket server has a race condition where it only starts listening for messages after the client has finished setting up.
This lets it queue up messages if the client isn't ready.

Reviewed By: passy

Differential Revision: D21256218

fbshipit-source-id: fa8481e11225cc473f03f3afbf4d0c50a48cec91
This commit is contained in:
John Knox
2020-04-27 07:11:50 -07:00
committed by Facebook GitHub Bot
parent 5c00cfe542
commit fcb89b94b5

View File

@@ -193,6 +193,7 @@ class Server extends EventEmitter {
case 'connect': {
const app = message.app;
const plugins = message.plugins;
let resolvedClient: Client | null = null;
const client = this.addConnection(
new WebsocketClientFlipperConnection(ws, app, plugins),
{
@@ -203,15 +204,18 @@ class Server extends EventEmitter {
sdk_version: 1,
},
{},
);
).then((c) => (resolvedClient = c));
clients[app] = client;
client.then((c) => {
ws.on('message', (m: any) => {
const parsed = JSON.parse(m.toString());
if (parsed.app === app) {
c.onMessage(JSON.stringify(parsed.payload));
const message = JSON.stringify(parsed.payload);
if (resolvedClient) {
resolvedClient.onMessage(message);
}
client.then((c) => c.onMessage(message));
}
});
});
break;
}