@@ -110,6 +110,7 @@ type CloseCallback = (event: CloseEvent) => void;
110110 * @param path the path to connect to, e.g. `/ws`
111111 * @param messageCallback the callback to handle incoming messages
112112 * @param readyCallback the callback to handle the connection being ready
113+ * @param closeCallback the callback to handle the connection being closed
113114 * @param errorCallback the callback to handle errors
114115 * @param heartbeatCallback the callback to handle heartbeats
115116 * @param heartbeatInterval the interval in seconds for sending heartbeats
@@ -118,6 +119,7 @@ function newWSConnection (
118119 path : string ,
119120 messageCallback : MessageCallback ,
120121 readyCallback : ReadyCallback | undefined ,
122+ closeCallback : CloseCallback | undefined ,
121123 errorCallback : ErrorCallback | undefined ,
122124 heartbeatCallback : HeartbeatCallback | undefined ,
123125 heartbeatInterval : number
@@ -154,6 +156,13 @@ function newWSConnection (
154156 if ( readyCallback ) readyCallback ( event )
155157 }
156158
159+ // Handle WebSocket connection closed
160+ socket . onclose = ( event : CloseEvent ) => {
161+ socket . clearKeepalive ( )
162+ if ( closeCallback ) closeCallback ( event )
163+ else console . debug ( 'WebSocket connection closed' , event )
164+ }
165+
157166 // Handle WebSocket message received
158167 socket . onmessage = ( event : MessageEvent ) => {
159168 let evt : WebSocketMessage
@@ -192,6 +201,7 @@ const WebSocketService = {
192201 * @param messageCallback message callback to handle incoming messages
193202 * @param heartbeatCallback heartbeat callback
194203 * @param readyCallback ready callback
204+ * @param closeCallback close callback
195205 * @param errorCallback error callback
196206 * @param heartbeatInterval heartbeat interval in seconds (defaults to 5)
197207 */
@@ -200,10 +210,11 @@ const WebSocketService = {
200210 messageCallback : MessageCallback ,
201211 heartbeatCallback : HeartbeatCallback ,
202212 readyCallback ?: ReadyCallback ,
213+ closeCallback ?: CloseCallback ,
203214 errorCallback ?: ErrorCallback ,
204215 heartbeatInterval : number = 5
205216 ) : KeepaliveWebSocket {
206- return newWSConnection ( path , messageCallback , readyCallback , errorCallback , heartbeatCallback , heartbeatInterval )
217+ return newWSConnection ( path , messageCallback , readyCallback , closeCallback , errorCallback , heartbeatCallback , heartbeatInterval )
207218 } ,
208219 /**
209220 * Connect to the event WebSocket, which provides direct access to the EventBus.
@@ -250,6 +261,7 @@ const WebSocketService = {
250261 extendedMessageCallback ,
251262 heartbeatCallback ,
252263 extendedReadyCallback ,
264+ undefined ,
253265 errorCallback
254266 )
255267
0 commit comments