Skip to content

Commit 49f52f4

Browse files
committed
Report WebSocket errors that occur after construction
1 parent 2429b73 commit 49f52f4

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

ui/frontend/websocket.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1+
async function reportWebSocketError() {
2+
try {
3+
await fetch('/nowebsocket', {
4+
method: 'post',
5+
headers: {
6+
'Content-Length': '0',
7+
},
8+
});
9+
} catch (e) {
10+
console.log('Error:', e);
11+
}
12+
}
13+
114
export default function openWebSocket(currentLocation: Location) {
215
try {
316
const wsProtocol = currentLocation.protocol === 'https:' ? 'wss://' : 'ws://';
417
const wsUri = [wsProtocol, currentLocation.host, '/websocket'].join('');
5-
return new WebSocket(wsUri);
18+
const ws = new WebSocket(wsUri);
19+
ws.addEventListener('error', () => reportWebSocketError());
20+
return ws;
621
} catch {
722
// WebSocket URL error or WebSocket is not supported by browser.
823
// Assume it's the second case since URL error is easy to notice.
9-
(async () => {
10-
try {
11-
await fetch('/nowebsocket', {
12-
method: 'post',
13-
headers: {
14-
'Content-Length': '0',
15-
},
16-
});
17-
} catch (e) {
18-
console.log('Error:', e);
19-
}
20-
})();
24+
reportWebSocketError();
2125
return null;
2226
}
2327
}

0 commit comments

Comments
 (0)