diff --git a/README.md b/README.md index eb8f7815..f582363b 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,10 @@ wsOpts = {
Destroy this wsProvider instance. Disconnects from the server and removes all event handlers.
wsProvider.on('sync', function(isSynced: boolean))
Add an event listener for the sync event that is fired when the client received content from the server.
- wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' })) + wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' | 'timeout' }))
Receive updates about the current connection status.
+
Note: The `timeout` event fires when there hasn't been any update on the WebSocket for `messageReconnectTimeout`, and the WebSocket + is marked for closing. The `disconnected` event is only fired after the closing handshake (which can get delayed when there is network disconenction).
wsProvider.on('connection-close', function(WSClosedEvent))
Fires when the underlying websocket connection is closed. It forwards the websocket event to this event handler.
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) {