From 2699f7921b4628d056926031bbdd379edd269055 Mon Sep 17 00:00:00 2001 From: Nils G <nils.gondermann@ruhr-uni-bochum.de> Date: Fri, 17 Apr 2020 13:29:15 +0200 Subject: [PATCH] Use secure websocket (wss) when the page is accesse by https --- modules/websocket_util.py | 7 ++++++- views/game/index.html | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/websocket_util.py b/modules/websocket_util.py index 187a348..17007da 100644 --- a/modules/websocket_util.py +++ b/modules/websocket_util.py @@ -7,4 +7,9 @@ def WEBSOCKET_SEND(_room, _type, _data): websocket_send('http://127.0.0.1:8888', _data, '67c371fd204fbcbf9742310d1a9d951bcca1aa86', _room.code) def GET_URL(_room): - return("ws://"+GET_HOST()+":8888/realtime/"+_room.code) + if current.request.env.wsgi_url_scheme == "https": + protocol = "wss" + else: + protocol = "ws" + + return(protocol+"://"+GET_HOST()+":8888/realtime/"+_room.code) diff --git a/views/game/index.html b/views/game/index.html index 535a5e5..cb2bf40 100644 --- a/views/game/index.html +++ b/views/game/index.html @@ -32,14 +32,14 @@ if ('WebSocket' in window) { var ws = new WebSocket("{{=GET_URL(room_record)}}"); ws.onopen = function () {}; - ws.onmessage = function(e){ + ws.onmessage = function(e) { set_info(e.data); render(name, role, JSON.parse(e.data)); }; ws.onerror = function(e) { set_error("{{=T('Error')}}: "+e); }; - ws.onclose =function () { + ws.onclose = function () { set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}."); }; } else { -- GitLab