feat(sync): Display a pannel to the user when disconnected.

This commit is contained in:
Alexis Métaireau 2024-10-25 15:32:45 +02:00
parent aac5185b16
commit b7ca961912
No known key found for this signature in database
GPG key ID: 1C21B876828E5FF2
3 changed files with 38 additions and 3 deletions

View file

@ -91,6 +91,7 @@ export class SyncEngine {
this._reconnectTimeout = null; this._reconnectTimeout = null;
this._reconnectDelay = RECONNECT_DELAY; this._reconnectDelay = RECONNECT_DELAY;
this.websocketConnected = true; this.websocketConnected = true;
this.updaters.map.update({ key: 'numberOfConnectedPeers' })
} }
reconnect() { reconnect() {
@ -102,6 +103,7 @@ export class SyncEngine {
if (this._reconnectDelay < MAX_RECONNECT_DELAY) { if (this._reconnectDelay < MAX_RECONNECT_DELAY) {
this._reconnectDelay = this._reconnectDelay * RECONNECT_DELAY_FACTOR this._reconnectDelay = this._reconnectDelay * RECONNECT_DELAY_FACTOR
} }
console.log("reconnecting now")
this.authenticate() this.authenticate()
}, this._reconnectDelay); }, this._reconnectDelay);
} }

View file

@ -4,6 +4,7 @@ const PING_INTERVAL = 30000;
export class WebSocketTransport { export class WebSocketTransport {
constructor(webSocketURI, authToken, messagesReceiver) { constructor(webSocketURI, authToken, messagesReceiver) {
this.receiver = messagesReceiver this.receiver = messagesReceiver
this.closeRequested = false
this.websocket = new WebSocket(webSocketURI) this.websocket = new WebSocket(webSocketURI)
@ -14,8 +15,11 @@ export class WebSocketTransport {
this.websocket.addEventListener('message', this.onMessage.bind(this)) this.websocket.addEventListener('message', this.onMessage.bind(this))
this.websocket.onclose = () => { this.websocket.onclose = () => {
console.log("websocket closed") console.log("websocket closed")
if (!this.closeRequested) {
console.log("Not requested, reconnecting...")
this.receiver.reconnect() this.receiver.reconnect()
} }
}
// To ensure the connection is still alive, we send ping and expect pong back. // To ensure the connection is still alive, we send ping and expect pong back.
// Websocket provides a `ping` method to keep the connection alive, but it's // Websocket provides a `ping` method to keep the connection alive, but it's
@ -50,4 +54,9 @@ export class WebSocketTransport {
const encoded = JSON.stringify(message) const encoded = JSON.stringify(message)
this.websocket.send(encoded) this.websocket.send(encoded)
} }
close() {
this.closeRequested = true
this.websocket.close()
}
} }

View file

@ -13,7 +13,7 @@ L.Map.mergeOptions({
// we cannot rely on this because of the y is overriden by Leaflet // we cannot rely on this because of the y is overriden by Leaflet
// See https://github.com/Leaflet/Leaflet/pull/9201 // See https://github.com/Leaflet/Leaflet/pull/9201
// And let's remove this -y when this PR is merged and released. // And let's remove this -y when this PR is merged and released.
demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, '-y': 181, r: '' }, demoTileInfos: { 's': 'a', 'z': 9, 'x': 265, 'y': 181, '-y': 181, 'r': '' },
licences: [], licences: [],
licence: '', licence: '',
enableMarkerDraw: true, enableMarkerDraw: true,
@ -75,7 +75,12 @@ U.Map = L.Map.extend({
}) })
const websocketURI = this.options.websocketURI const websocketURI = this.options.websocketURI
this.sync_engine = new U.SyncEngine(this, websocketTokenURI, websocketURI, this.server) this.sync_engine = new U.SyncEngine(
this,
websocketTokenURI,
websocketURI,
this.server
)
this.sync = this.sync_engine.proxy(this) this.sync = this.sync_engine.proxy(this)
this.initLoader() this.initLoader()
@ -231,6 +236,25 @@ U.Map = L.Map.extend({
}, },
render: function (fields) { 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')) { if (fields.includes('numberOfConnectedPeers')) {
this.renderEditToolbar() this.renderEditToolbar()
this.propagate() this.propagate()