File tree Expand file tree Collapse file tree 1 file changed +17
-13
lines changed Expand file tree Collapse file tree 1 file changed +17
-13
lines changed Original file line number Diff line number Diff line change
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
+
1
14
export default function openWebSocket ( currentLocation : Location ) {
2
15
try {
3
16
const wsProtocol = currentLocation . protocol === 'https:' ? 'wss://' : 'ws://' ;
4
17
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 ;
6
21
} catch {
7
22
// WebSocket URL error or WebSocket is not supported by browser.
8
23
// 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 ( ) ;
21
25
return null ;
22
26
}
23
27
}
You can’t perform that action at this time.
0 commit comments