diff --git a/README.md b/README.md index eb8f7815..f582363b 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,10 @@ wsOpts = {
wsProvider.on('sync', function(isSynced: boolean))
wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' }))
+ wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' | 'timeout' }))
wsProvider.on('connection-close', function(WSClosedEvent))
wsProvider.on('connection-error', function(WSErrorEvent))
diff --git a/src/y-websocket.js b/src/y-websocket.js
index bbabd3f8..6d3ba01e 100644
--- a/src/y-websocket.js
+++ b/src/y-websocket.js
@@ -372,6 +372,14 @@ export class WebsocketProvider extends Observable {
// no message received in a long time - not even your own awareness
// updates (which are updated every 15 seconds)
/** @type {WebSocket} */ (this.ws).close()
+ // Closing a WebSocket instance, especially in browsers will not
+ // cause it to immediately close, instead, it initiates the
+ // closing handshake (https://www.rfc-editor.org/rfc/rfc6455.html#section-1.4)
+ // which will delay firing of 'close' event.
+ // A dedicated 'timeout' status update can be used to detect this state
+ this.emit('status', [{
+ status: 'timeout'
+ }]);
}
}, messageReconnectTimeout / 10))
if (connect) {