@@ -451,9 +451,9 @@ export interface Client extends Disposable {
451
451
/**
452
452
* Terminates the WebSocket abruptly and immediately.
453
453
*
454
- * A close event `4499: Terminated` is issued to the current WebSocket and an
455
- * artificial `{ code: 4499, reason: 'Terminated', wasClean: false }` close-event-like
456
- * object is immediately emitted without waiting for the one coming from `WebSocket.onclose`.
454
+ * A close event `4499: Terminated` is issued to the current WebSocket and a
455
+ * syntetic { @link TerminatedCloseEvent} is immediately emitted without waiting for
456
+ * the one coming from `WebSocket.onclose`.
457
457
*
458
458
* Terminating is not considered fatal and a connection retry will occur as expected.
459
459
*
@@ -664,7 +664,7 @@ export function createClient<
664
664
clearTimeout ( queuedPing ) ;
665
665
denied ( errOrEvent ) ;
666
666
667
- if ( isLikeCloseEvent ( errOrEvent ) && errOrEvent . code === 4499 ) {
667
+ if ( errOrEvent instanceof TerminatedCloseEvent ) {
668
668
socket . close ( 4499 , 'Terminated' ) ; // close event is artificial and emitted manually, see `Client.terminate()` below
669
669
socket . onerror = null ;
670
670
socket . onclose = null ;
@@ -1061,16 +1061,29 @@ export function createClient<
1061
1061
terminate ( ) {
1062
1062
if ( connecting ) {
1063
1063
// only if there is a connection
1064
- emitter . emit ( 'closed' , {
1065
- code : 4499 ,
1066
- reason : 'Terminated' ,
1067
- wasClean : false ,
1068
- } ) ;
1064
+ emitter . emit ( 'closed' , new TerminatedCloseEvent ( ) ) ;
1069
1065
}
1070
1066
} ,
1071
1067
} ;
1072
1068
}
1073
1069
1070
+ /**
1071
+ * A syntetic close event `4499: Terminated` is issued to the current to immediately
1072
+ * close the connection without waiting for the one coming from `WebSocket.onclose`.
1073
+ *
1074
+ * Terminating is not considered fatal and a connection retry will occur as expected.
1075
+ *
1076
+ * Useful in cases where the WebSocket is stuck and not emitting any events;
1077
+ * can happen on iOS Safari, see: https://github.com/enisdenjo/graphql-ws/discussions/290.
1078
+ */
1079
+ export class TerminatedCloseEvent extends Error {
1080
+ public name = 'TerminatedCloseEvent' ;
1081
+ public message = '4499: Terminated' ;
1082
+ public code = 4499 ;
1083
+ public reason = 'Terminated' ;
1084
+ public wasClean = false ;
1085
+ }
1086
+
1074
1087
/** Minimal close event interface required by the lib for error and socket close handling. */
1075
1088
interface LikeCloseEvent {
1076
1089
/** Returns the WebSocket connection close code provided by the server. */
0 commit comments