Skip to content

Commit 3982a2c

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Log Abnormal Closes to Metro Websocket
Summary: We are running into a group seeing frequent disconnects from Metro in a specific office. These are surfaced (at least on iOS) as websocket closures, without a prior websocket error. WebSocket closure can be for a variety of reasons, and the spec for a CloseEvent is to include fields `wasClean`, `code`, and `reason`, with `code` having the most well-defined meaning. This change makes it so that we emit extra context when the websocket is closed. That should help inform developers the reason behind any close that may be abnormal. Changelog: [General][Added] - Log Abnormal Closes to Metro Websocket Reviewed By: motiz88 Differential Revision: D40660765 fbshipit-source-id: ef606d8d809af1c697a78eb00cc5666c29a8bca3
1 parent 279cfec commit 3982a2c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Libraries/Utilities/HMRClient.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,23 @@ Error: ${e.message}`;
241241
}
242242
});
243243

244-
client.on('close', data => {
244+
client.on('close', closeEvent => {
245245
LoadingView.hide();
246-
setHMRUnavailableReason('Disconnected from Metro.');
246+
247+
// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1
248+
// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5
249+
const isNormalOrUnsetCloseReason =
250+
closeEvent.code === 1000 ||
251+
closeEvent.code === 1005 ||
252+
closeEvent.code == null;
253+
254+
if (isNormalOrUnsetCloseReason) {
255+
setHMRUnavailableReason('Disconnected from Metro.');
256+
} else {
257+
setHMRUnavailableReason(
258+
`Disconnected from Metro (${closeEvent.code}: "${closeEvent.reason}").`,
259+
);
260+
}
247261
});
248262

249263
if (isEnabled) {

Libraries/WebSocket/WebSocket.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const CLOSED = 3;
4343

4444
const CLOSE_NORMAL = 1000;
4545

46+
// Abnormal closure where no code is provided in a control frame
47+
// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5
48+
const CLOSE_ABNORMAL = 1006;
49+
4650
const WEBSOCKET_EVENTS = ['close', 'error', 'message', 'open'];
4751

4852
let nextWebSocketId = 0;
@@ -260,6 +264,7 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
260264
new WebSocketEvent('close', {
261265
code: ev.code,
262266
reason: ev.reason,
267+
// TODO: missing `wasClean` (exposed on iOS as `clean` but missing on Android)
263268
}),
264269
);
265270
this._unregisterEvents();
@@ -277,7 +282,9 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
277282
);
278283
this.dispatchEvent(
279284
new WebSocketEvent('close', {
280-
message: ev.message,
285+
code: CLOSE_ABNORMAL,
286+
reason: ev.message,
287+
// TODO: Expose `wasClean`
281288
}),
282289
);
283290
this._unregisterEvents();

0 commit comments

Comments
 (0)