Skip to content

Commit

Permalink
feat(sync): Display a pannel to the user when disconnected.
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Oct 25, 2024
1 parent 62a3ba7 commit 548b9eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions umap/static/umap/js/modules/sync/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class SyncEngine {
this._reconnectTimeout = null;
this._reconnectDelay = RECONNECT_DELAY;
this.websocketConnected = true;
this.updaters.map.update({ key: 'numberOfConnectedPeers' })
}

reconnect() {
Expand All @@ -102,6 +103,7 @@ export class SyncEngine {
if (this._reconnectDelay < MAX_RECONNECT_DELAY) {
this._reconnectDelay = this._reconnectDelay * RECONNECT_DELAY_FACTOR
}
console.log("reconnecting now")
this.authenticate()
}, this._reconnectDelay);
}
Expand Down
11 changes: 10 additions & 1 deletion umap/static/umap/js/modules/sync/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const PING_INTERVAL = 30000;
export class WebSocketTransport {
constructor(webSocketURI, authToken, messagesReceiver) {
this.receiver = messagesReceiver
this.closeRequested = false

this.websocket = new WebSocket(webSocketURI)

Expand All @@ -14,7 +15,10 @@ export class WebSocketTransport {
this.websocket.addEventListener('message', this.onMessage.bind(this))
this.websocket.onclose = () => {
console.log("websocket closed")
this.receiver.reconnect()
if (!this.closeRequested) {
console.log("Not requested, reconnecting...")
this.receiver.reconnect()
}
}

// To ensure the connection is still alive, we send ping and expect pong back.
Expand Down Expand Up @@ -50,4 +54,9 @@ export class WebSocketTransport {
const encoded = JSON.stringify(message)
this.websocket.send(encoded)
}

close() {
this.closeRequested = true
this.websocket.close()
}
}
13 changes: 13 additions & 0 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ U.Map = L.Map.extend({
},

render: function (fields) {
if (this.options.syncEnabled === true) {
if (!this.sync.websocketConnected) {
const template = `
<h3><i class="icon icon-16"></i><span>${L._('Disconnected')}</span></h3>
<p>
${L._('This map has enabled real-time synchronization with other users, but you are currently disconnected. It will automatically reconnect when ready.')}
</p>
`
this.dialog.open({ template: template, className: 'dark', cancel: false, accept: false })
} else {
this.dialog.close()
}
}

if (fields.includes('numberOfConnectedPeers')) {
this.renderEditToolbar()
Expand Down

0 comments on commit 548b9eb

Please sign in to comment.