Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ wsOpts = {
<dd>Destroy this wsProvider instance. Disconnects from the server and removes all event handlers.</dd>
<b><code>wsProvider.on('sync', function(isSynced: boolean))</code></b>
<dd>Add an event listener for the sync event that is fired when the client received content from the server.</dd>
<b><code>wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' }))</code></b>
<b><code>wsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' | 'timeout' }))</code></b>
<dd>Receive updates about the current connection status.</dd>
<dd>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).</dd>
<b><code>wsProvider.on('connection-close', function(WSClosedEvent))</code></b>
<dd>Fires when the underlying websocket connection is closed. It forwards the websocket event to this event handler.</dd>
<b><code>wsProvider.on('connection-error', function(WSErrorEvent))</code></b>
Expand Down
8 changes: 8 additions & 0 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down