From b058b34e881c4dacd3341266989ec05750624367 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 9 May 2021 00:21:23 -0400 Subject: [PATCH] frontend: Use websockets over TLS and https if connection secure This is super exciting! I finally got around to configuring relayd(1) (an OpenBSD base relay that I'm using to distribute load between alexkarle.com and euchre.live) to do full TLS acceleration/termination so that euchre.live can be run over HTTPS! To handle this properly (without changing the dev workflow), the frontend now needs to inspect the protocol and choose wss+https or ws+http accordingly. This patch does just that. --- assets/app.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/assets/app.js b/assets/app.js index b141c1b..bbeb69b 100644 --- a/assets/app.js +++ b/assets/app.js @@ -30,8 +30,13 @@ class App extends React.Component { tableList: [] }; const host = window.location.host; - socketAddr = 'ws://' + host + '/play'; - tablesAddr = 'http://' + host + '/tables'; + if (window.location.protocol === 'https:') { + socketAddr = 'wss://' + host + '/play'; + tablesAddr = 'https://' + host + '/tables'; + } else { + socketAddr = 'ws://' + host + '/play'; + tablesAddr = 'http://' + host + '/tables'; + } if (tableDebug) { client = new W3CWebSocket(socketAddr); client.onmessage = (event) => this.processResponse(event); -- libgit2 1.1.1