Set websocket url to /ws

The routing is desided by protocol
This commit is contained in:
Oskar Hahn 2018-11-04 17:13:24 +01:00 committed by FinnStutzenstein
parent a2d736bae9
commit 8ee9fb1742
5 changed files with 18 additions and 37 deletions

View File

@ -11,7 +11,7 @@
"target": "http://localhost:8000",
"secure": false
},
"/ws/site/": {
"/ws/": {
"target": "ws://localhost:8000",
"secure": false,
"ws": true

View File

@ -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;
}
}

View File

@ -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/'
}
});
};

View File

@ -9,7 +9,7 @@ application = ProtocolTypeRouter({
# WebSocket chat handler
"websocket": AuthMiddlewareStack(
URLRouter([
url(r"^ws/site/$", SiteConsumer),
url(r"^ws/$", SiteConsumer),
])
)
})

View File

@ -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()