Skip to content

Commit

Permalink
Support ws or wss proto
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemcdermott committed Dec 11, 2020
1 parent 6c59111 commit 39fe4f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion awx/ui_next/src/screens/Job/JobOutput/JobOutput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ const OutputFooter = styled.div`

let ws;
function connectJobSocket({ type, id }, onMessage) {
ws = new WebSocket(`wss://${window.location.host}/websocket/`);
ws = new WebSocket(
`${window.location.protocol === 'http:' ? 'ws:' : 'wss:'}//${
window.location.host
}/websocket/`
);

ws.onopen = () => {
const xrftoken = `; ${document.cookie}`
Expand Down
7 changes: 7 additions & 0 deletions awx/ui_next/src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ global.console = {
// and so this mock ensures that we don't encounter a reference error
// when running the tests
global.__webpack_nonce__ = null;

// This is to make websocket-using components behave as if they're connecting via
// wss, which is the default behavior being tested in most websocket tests
Object.defineProperty(window, 'location', {
value: new URL('https://localhost'),
});
window.location.replace = jest.fn();
6 changes: 5 additions & 1 deletion awx/ui_next/src/util/useWebsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export default function useWebsocket(subscribeGroups) {
const ws = useRef(null);

useEffect(function setupSocket() {
ws.current = new WebSocket(`wss://${window.location.host}/websocket/`);
ws.current = new WebSocket(
`${window.location.protocol === 'http:' ? 'ws:' : 'wss:'}//${
window.location.host
}/websocket/`
);

const connect = () => {
const xrftoken = `; ${document.cookie}`
Expand Down

0 comments on commit 39fe4f0

Please sign in to comment.