mirror of
https://github.com/django/daphne.git
synced 2025-07-30 16:59:46 +03:00
Allow relative url in WebsocketBridge.connect()
(#567)
* Add support for relative urls in javascript wrapper * recompile static file * add js smoke test for relative urls * update docs to show relative urls
This commit is contained in:
parent
4323a64e33
commit
10398780a3
|
@ -270,12 +270,18 @@ var WebSocketBridge = function () {
|
||||||
key: 'connect',
|
key: 'connect',
|
||||||
value: function connect(url, protocols, options) {
|
value: function connect(url, protocols, options) {
|
||||||
var _url = void 0;
|
var _url = void 0;
|
||||||
|
// Use wss:// if running on https://
|
||||||
|
var scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
||||||
|
var base_url = scheme + '://' + window.location.host;
|
||||||
if (url === undefined) {
|
if (url === undefined) {
|
||||||
// Use wss:// if running on https://
|
_url = base_url;
|
||||||
var scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
|
||||||
_url = scheme + '://' + window.location.host + '/ws';
|
|
||||||
} else {
|
} else {
|
||||||
_url = url;
|
// Support relative URLs
|
||||||
|
if (url[0] == '/') {
|
||||||
|
_url = '' + base_url + url;
|
||||||
|
} else {
|
||||||
|
_url = url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this._socket = new _reconnectingWebsocket2.default(_url, protocols, options);
|
this._socket = new _reconnectingWebsocket2.default(_url, protocols, options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ specific integration on top of it.
|
||||||
To process messages::
|
To process messages::
|
||||||
|
|
||||||
const webSocketBridge = new channels.WebSocketBridge();
|
const webSocketBridge = new channels.WebSocketBridge();
|
||||||
webSocketBridge.connect();
|
webSocketBridge.connect('/ws/');
|
||||||
webSocketBridge.listen(function(action, stream) {
|
webSocketBridge.listen(function(action, stream) {
|
||||||
console.log(action, stream);
|
console.log(action, stream);
|
||||||
});
|
});
|
||||||
|
@ -35,7 +35,7 @@ To send messages, use the `send` method::
|
||||||
To demultiplex specific streams::
|
To demultiplex specific streams::
|
||||||
|
|
||||||
webSocketBridge.connect();
|
webSocketBridge.connect();
|
||||||
webSocketBridge.listen();
|
webSocketBridge.listen('/ws/');
|
||||||
webSocketBridge.demultiplex('mystream', function(action, stream) {
|
webSocketBridge.demultiplex('mystream', function(action, stream) {
|
||||||
console.log(action, stream);
|
console.log(action, stream);
|
||||||
});
|
});
|
||||||
|
|
|
@ -36,12 +36,18 @@ export class WebSocketBridge {
|
||||||
*/
|
*/
|
||||||
connect(url, protocols, options) {
|
connect(url, protocols, options) {
|
||||||
let _url;
|
let _url;
|
||||||
|
// Use wss:// if running on https://
|
||||||
|
const scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
||||||
|
const base_url = `${scheme}://${window.location.host}`;
|
||||||
if (url === undefined) {
|
if (url === undefined) {
|
||||||
// Use wss:// if running on https://
|
_url = base_url;
|
||||||
const scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
|
||||||
_url = `${scheme}://${window.location.host}/ws`;
|
|
||||||
} else {
|
} else {
|
||||||
_url = url;
|
// Support relative URLs
|
||||||
|
if (url[0] == '/') {
|
||||||
|
_url = `${base_url}${url}`;
|
||||||
|
} else {
|
||||||
|
_url = url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this._socket = new ReconnectingWebSocket(_url, protocols, options);
|
this._socket = new ReconnectingWebSocket(_url, protocols, options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,11 @@ describe('WebSocketBridge', () => {
|
||||||
const webSocketBridge = new WebSocketBridge();
|
const webSocketBridge = new WebSocketBridge();
|
||||||
webSocketBridge.connect('ws://localhost');
|
webSocketBridge.connect('ws://localhost');
|
||||||
});
|
});
|
||||||
|
it('Supports relative urls', () => {
|
||||||
|
const webSocketBridge = new WebSocketBridge();
|
||||||
|
webSocketBridge.connect('/somepath/');
|
||||||
|
});
|
||||||
|
|
||||||
it('Processes messages', () => {
|
it('Processes messages', () => {
|
||||||
const webSocketBridge = new WebSocketBridge();
|
const webSocketBridge = new WebSocketBridge();
|
||||||
const myMock = jest.fn();
|
const myMock = jest.fn();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user