feat(sync): Add WebSocket specific settings

`WEBSOCKET_ENABLED`, `WEBSOCKET_HOST`, `WEBSOCKET_PORT` and
`WEBSOCKET_URI` have been added.

They are being transmitted to the client via the `map.options`.
When `WEBSOCKET_ENABLED` is set to `False`, the client doesn't have
the ability to be synced.
This commit is contained in:
Alexis Métaireau 2024-04-30 18:45:07 +02:00
parent 5e692d2280
commit 32a4f3feda
4 changed files with 37 additions and 14 deletions

View file

@ -306,3 +306,10 @@ LOGGING = {
}, },
}, },
} }
# WebSocket configuration
WEBSOCKET_ENABLED = False
WEBSOCKET_HOST = "localhost"
WEBSOCKET_PORT = 8001
WEBSOCKET_URI = "ws://localhost:8001"

View file

@ -254,6 +254,7 @@ U.Map = L.Map.extend({
}, },
initSyncEngine: async function () { initSyncEngine: async function () {
if (this.options.websocketEnabled == false) return
console.log('this.options.syncEnabled', this.options.syncEnabled) console.log('this.options.syncEnabled', this.options.syncEnabled)
if (this.options.syncEnabled != true) { if (this.options.syncEnabled != true) {
this.sync.stop() this.sync.stop()
@ -265,7 +266,7 @@ U.Map = L.Map.extend({
`/map/${this.options.umap_id}/ws-token/` `/map/${this.options.umap_id}/ws-token/`
) )
if (!error) { if (!error) {
this.sync.start('ws://localhost:8001', response.token) this.sync.start(this.options.websocketURI, response.token)
} }
} }
}, },
@ -1545,9 +1546,14 @@ U.Map = L.Map.extend({
editCaption: function () { editCaption: function () {
if (!this.editEnabled) return if (!this.editEnabled) return
if (this.options.editMode !== 'advanced') return if (this.options.editMode !== 'advanced') return
const container = L.DomUtil.create('div', 'umap-edit-container'), const container = L.DomUtil.create('div', 'umap-edit-container')
metadataFields = ['options.name', 'options.description', 'options.syncEnabled'], const metadataFields = ['options.name', 'options.description']
title = L.DomUtil.create('h3', '', container)
if (this.options.websocketEnabled) {
metadataFields.push('options.syncEnabled')
}
const title = L.DomUtil.create('h3', '', container)
title.textContent = L._('Edit map details') title.textContent = L._('Edit map details')
const builder = new U.FormBuilder(this, metadataFields, { const builder = new U.FormBuilder(this, metadataFields, {
className: 'map-metadata', className: 'map-metadata',

View file

@ -326,9 +326,9 @@ class UserDownload(DetailView, SearchMixin):
zip_file.writestr(file_name, geojson_file.getvalue()) zip_file.writestr(file_name, geojson_file.getvalue())
response = HttpResponse(zip_buffer.getvalue(), content_type="application/zip") response = HttpResponse(zip_buffer.getvalue(), content_type="application/zip")
response[ response["Content-Disposition"] = (
"Content-Disposition" 'attachment; filename="umap_backup_complete.zip"'
] = 'attachment; filename="umap_backup_complete.zip"' )
return response return response
@ -503,6 +503,8 @@ class MapDetailMixin:
], ],
"umap_version": VERSION, "umap_version": VERSION,
"featuresHaveOwner": settings.UMAP_DEFAULT_FEATURES_HAVE_OWNERS, "featuresHaveOwner": settings.UMAP_DEFAULT_FEATURES_HAVE_OWNERS,
"websocketEnabled": settings.WEBSOCKET_ENABLED,
"websocketURI": settings.WEBSOCKET_URI,
} }
created = bool(getattr(self, "object", None)) created = bool(getattr(self, "object", None))
if (created and self.object.owner) or (not created and not user.is_anonymous): if (created and self.object.owner) or (not created and not user.is_anonymous):
@ -623,8 +625,8 @@ class MapView(MapDetailMixin, PermissionsMixin, DetailView):
def get_datalayers(self): def get_datalayers(self):
return [ return [
l.metadata(self.request.user, self.request) dl.metadata(self.request.user, self.request)
for l in self.object.datalayer_set.all() for dl in self.object.datalayer_set.all()
] ]
@property @property
@ -674,9 +676,9 @@ class MapDownload(DetailView):
def render_to_response(self, context, *args, **kwargs): def render_to_response(self, context, *args, **kwargs):
umapjson = self.object.generate_umapjson(self.request) umapjson = self.object.generate_umapjson(self.request)
response = simple_json_response(**umapjson) response = simple_json_response(**umapjson)
response[ response["Content-Disposition"] = (
"Content-Disposition" f'attachment; filename="umap_backup_{self.object.slug}.umap"'
] = f'attachment; filename="umap_backup_{self.object.slug}.umap"' )
return response return response

View file

@ -98,8 +98,16 @@ async def handler(websocket):
async def main(): async def main():
print("WebSocket server waiting for connections") if not settings.WEBSOCKET_ENABLED:
async with serve(handler, "localhost", 8001): print("WEBSOCKET_ENABLED should be set to True to run the WebSocket Server")
exit(1)
async with serve(handler, settings.WEBSOCKET_HOST, settings.WEBSOCKET_PORT):
print(
(
f"Waiting for connections on {settings.WEBSOCKET_HOST}:{settings.WEBSOCKET_PORT}"
)
)
await asyncio.Future() # run forever await asyncio.Future() # run forever