From 8ee9fb174284cf4e5a6cb688d1c2d0dbe34c9527 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sun, 4 Nov 2018 17:13:24 +0100 Subject: [PATCH] Set websocket url to /ws The routing is desided by protocol --- client/proxy.conf.json | 2 +- .../app/core/services/websocket.service.ts | 45 ++++++------------- client/src/karma.conf.js | 2 +- openslides/routing.py | 2 +- tests/integration/utils/test_consumers.py | 4 +- 5 files changed, 18 insertions(+), 37 deletions(-) diff --git a/client/proxy.conf.json b/client/proxy.conf.json index 2373248d9..74366d271 100644 --- a/client/proxy.conf.json +++ b/client/proxy.conf.json @@ -11,7 +11,7 @@ "target": "http://localhost:8000", "secure": false }, - "/ws/site/": { + "/ws/": { "target": "ws://localhost:8000", "secure": false, "ws": true diff --git a/client/src/app/core/services/websocket.service.ts b/client/src/app/core/services/websocket.service.ts index b36d2ce8f..94417a88e 100644 --- a/client/src/app/core/services/websocket.service.ts +++ b/client/src/app/core/services/websocket.service.ts @@ -1,5 +1,4 @@ import { Injectable, NgZone, EventEmitter } from '@angular/core'; -import { Router } from '@angular/router'; import { Observable, Subject } from 'rxjs'; import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material'; import { TranslateService } from '@ngx-translate/core'; @@ -76,13 +75,11 @@ export class WebsocketService { /** * Constructor that handles the router - * @param router the URL Router * @param matSnackBar * @param zone * @param translate */ public constructor( - private router: Router, private matSnackBar: MatSnackBar, private zone: NgZone, public translate: TranslateService @@ -117,11 +114,12 @@ export class WebsocketService { } // Create the websocket - const socketProtocol = this.getWebSocketProtocol(); - const socketServer = window.location.hostname + ':' + window.location.port; - const socketPath = this.getWebSocketPath(queryParams); - console.log('connect to', socketProtocol + socketServer + socketPath); - this.websocket = new WebSocket(socketProtocol + socketServer + socketPath); + let socketPath = location.protocol === 'https' ? 'wss://' : 'ws://'; + socketPath += window.location.hostname + ':' + window.location.port + '/ws/'; + socketPath += this.formatQueryParams(queryParams); + + console.log('connect to', socketPath); + this.websocket = new WebSocket(socketPath); // connection established. If this connect attept was a retry, // The error notice will be removed and the reconnectSubject is published. @@ -229,21 +227,15 @@ export class WebsocketService { } /** - * Delegates to socket-path for either the side or projector websocket. + * Formats query params for the url. + * @param queryParams + * @returns the formatted query params as string */ - private getWebSocketPath(queryParams: QueryParams = {}): string { - // currentRoute does not end with '/' - const currentRoute = this.router.url; - let path: string; - if (currentRoute.includes('/projector') || currentRoute.includes('/real-projector')) { - path = '/ws/projector/'; - } else { - path = '/ws/site/'; - } - + private formatQueryParams(queryParams: QueryParams = {}): string { + let params = ''; const keys: string[] = Object.keys(queryParams); if (keys.length > 0) { - path += + params = '?' + keys .map(key => { @@ -251,17 +243,6 @@ export class WebsocketService { }) .join('&'); } - return path; - } - - /** - * returns the desired websocket protocol - */ - private getWebSocketProtocol(): string { - if (location.protocol === 'https') { - return 'wss://'; - } else { - return 'ws://'; - } + return params; } } diff --git a/client/src/karma.conf.js b/client/src/karma.conf.js index 5f8f30cc8..ecf7ad9e7 100644 --- a/client/src/karma.conf.js +++ b/client/src/karma.conf.js @@ -31,7 +31,7 @@ module.exports = function(config) { '/apps/': 'http://localhost:8000/apps/', '/media/': 'http://localhost:8000/media/', '/rest/': 'http://localhost:8000/rest/', - '/ws/site/': 'ws://localhost:8000/ws/site' + '/ws/': 'ws://localhost:8000/' } }); }; diff --git a/openslides/routing.py b/openslides/routing.py index fb693c89d..a6e41c131 100644 --- a/openslides/routing.py +++ b/openslides/routing.py @@ -9,7 +9,7 @@ application = ProtocolTypeRouter({ # WebSocket chat handler "websocket": AuthMiddlewareStack( URLRouter([ - url(r"^ws/site/$", SiteConsumer), + url(r"^ws/$", SiteConsumer), ]) ) }) diff --git a/tests/integration/utils/test_consumers.py b/tests/integration/utils/test_consumers.py index 64890029c..edbccff14 100644 --- a/tests/integration/utils/test_consumers.py +++ b/tests/integration/utils/test_consumers.py @@ -51,7 +51,7 @@ async def get_communicator(): nonlocal communicator # use the outer communicator variable if query_string: query_string = "?{}".format(query_string) - communicator = WebsocketCommunicator(application, "/ws/site/{}".format(query_string)) + communicator = WebsocketCommunicator(application, "/ws/{}".format(query_string)) return communicator yield get_communicator @@ -181,7 +181,7 @@ async def test_with_user(): session.save() scn = settings.SESSION_COOKIE_NAME cookies = (b'cookie', '{}={}'.format(scn, session.session_key).encode()) - communicator = WebsocketCommunicator(application, "/ws/site/", headers=[cookies]) + communicator = WebsocketCommunicator(application, "/ws/", headers=[cookies]) connected, __ = await communicator.connect()