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:
Flavio Curella 2017-03-23 18:30:28 -05:00 committed by Andrew Godwin
parent 4323a64e33
commit 10398780a3
4 changed files with 27 additions and 10 deletions

View File

@ -270,12 +270,18 @@ var WebSocketBridge = function () {
key: 'connect',
value: function connect(url, protocols, options) {
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) {
// Use wss:// if running on https://
var scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
_url = scheme + '://' + window.location.host + '/ws';
_url = base_url;
} else {
_url = url;
// Support relative URLs
if (url[0] == '/') {
_url = '' + base_url + url;
} else {
_url = url;
}
}
this._socket = new _reconnectingWebsocket2.default(_url, protocols, options);
}

View File

@ -23,7 +23,7 @@ specific integration on top of it.
To process messages::
const webSocketBridge = new channels.WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.connect('/ws/');
webSocketBridge.listen(function(action, stream) {
console.log(action, stream);
});
@ -35,7 +35,7 @@ To send messages, use the `send` method::
To demultiplex specific streams::
webSocketBridge.connect();
webSocketBridge.listen();
webSocketBridge.listen('/ws/');
webSocketBridge.demultiplex('mystream', function(action, stream) {
console.log(action, stream);
});

View File

@ -36,12 +36,18 @@ export class WebSocketBridge {
*/
connect(url, protocols, options) {
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) {
// Use wss:// if running on https://
const scheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
_url = `${scheme}://${window.location.host}/ws`;
_url = base_url;
} else {
_url = url;
// Support relative URLs
if (url[0] == '/') {
_url = `${base_url}${url}`;
} else {
_url = url;
}
}
this._socket = new ReconnectingWebSocket(_url, protocols, options);
}

View File

@ -16,6 +16,11 @@ describe('WebSocketBridge', () => {
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect('ws://localhost');
});
it('Supports relative urls', () => {
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect('/somepath/');
});
it('Processes messages', () => {
const webSocketBridge = new WebSocketBridge();
const myMock = jest.fn();