From 54630b459cca253d597bfbaec55ff11ca1c4340e Mon Sep 17 00:00:00 2001
From: Tim Fechner <tim.fechner@hs-hannover.de>
Date: Fri, 26 Aug 2016 12:19:17 +0200
Subject: [PATCH] Fix broken websockets

---
 salt_observer/assets/js/events.js         | 2 +-
 salt_observer/private_settings.example.py | 2 +-
 salt_observer/static/js/events.min.js     | 2 +-
 salt_observer/templates/events.html       | 2 ++
 salt_observer/views.py                    | 8 ++++++++
 5 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/salt_observer/assets/js/events.js b/salt_observer/assets/js/events.js
index 417032c..a075e91 100644
--- a/salt_observer/assets/js/events.js
+++ b/salt_observer/assets/js/events.js
@@ -2,7 +2,7 @@ $(function() {
 
     // ATTENTION: set the token before importing this script
 
-    var socket = new WebSocket('ws://localhost:8002/all_events/' + token);
+    var socket = new WebSocket('wss://' + host + ':' + port + '/all_events/' + token);
 
     // Get Salt's "real time" event stream.
     socket.onopen = function() {
diff --git a/salt_observer/private_settings.example.py b/salt_observer/private_settings.example.py
index a77bc23..1d1dd0d 100644
--- a/salt_observer/private_settings.example.py
+++ b/salt_observer/private_settings.example.py
@@ -33,7 +33,7 @@ SESSION_COOKIE_NAME = "cookie_session_salt_observer"
 SALT_API = {
     'cherrypy': {
         'host': 'localhost',
-        'port': 8989,
+        'port': 8001,
         'protocol': 'http',
     },
     'tornado': {
diff --git a/salt_observer/static/js/events.min.js b/salt_observer/static/js/events.min.js
index 3090029..031ab67 100644
--- a/salt_observer/static/js/events.min.js
+++ b/salt_observer/static/js/events.min.js
@@ -1 +1 @@
-$(function(){function t(t){var e=$("#event-template").clone();e.removeAttr("id"),e.find(".icon").html('<i class="fa fa-fw fa-2x fa-bug"></i>'),e.find(".title").text(t.data.id),e.find(".message").html("<pre>"+JSON.stringify(t.data)+"</pre>"),e.find(".tag").text(t.tag),e.find(".minion-id").text(t.data.id),$("#eventholder").prepend(e),setTimeout(function(){e.remove()},3e5)}function e(t){var e=$("#job-template").clone();e.removeAttr("id"),e.find(".timestamp").text(t.data._stamp),e.find(".tag").text(t.tag),$("#jobholder").prepend(e),setTimeout(function(){e.remove()},6e4)}var n=new WebSocket("ws://localhost:8002/all_events/"+token);n.onopen=function(){n.send("websocket client ready"),console.log("Connected!"),$("#conn_status").html('<i class="fa fa-fw fa-check"></i> Connected!')},n.onerror=function(t){console.debug("Error!",t),$("#conn_status").html('<i class="fa fa-fw fa-times"></i> Connection failed!')},n.onmessage=function(n){console.log(n);var o=JSON.parse(n.data.substring(6));o.tag.startsWith("salt/job/")?e(o):t(o)}});
\ No newline at end of file
+$(function(){function t(t){var e=$("#event-template").clone();e.removeAttr("id"),e.find(".icon").html('<i class="fa fa-fw fa-2x fa-bug"></i>'),e.find(".title").text(t.data.id),e.find(".message").html("<pre>"+JSON.stringify(t.data)+"</pre>"),e.find(".tag").text(t.tag),e.find(".minion-id").text(t.data.id),$("#eventholder").prepend(e),setTimeout(function(){e.remove()},3e5)}function e(t){var e=$("#job-template").clone();e.removeAttr("id"),e.find(".timestamp").text(t.data._stamp),e.find(".tag").text(t.tag),$("#jobholder").prepend(e),setTimeout(function(){e.remove()},6e4)}var n=new WebSocket("wss://"+host+":"+port+"/all_events/"+token);n.onopen=function(){n.send("websocket client ready"),console.log("Connected!"),$("#conn_status").html('<i class="fa fa-fw fa-check"></i> Connected!')},n.onerror=function(t){console.debug("Error!",t),$("#conn_status").html('<i class="fa fa-fw fa-times"></i> Connection failed!')},n.onmessage=function(n){console.log(n);var o=JSON.parse(n.data.substring(6));o.tag.startsWith("salt/job/")?e(o):t(o)}});
\ No newline at end of file
diff --git a/salt_observer/templates/events.html b/salt_observer/templates/events.html
index 4faa463..76a02b8 100644
--- a/salt_observer/templates/events.html
+++ b/salt_observer/templates/events.html
@@ -11,6 +11,8 @@
 
 {% block post-script %}
     <script>
+        var host = "{{ tornado_host }}";
+        var port = "{{ tornado_port }}";
         var token = "{{ request.session.salt_tornado_token }}";
     </script>
     <script src="{% static 'js/events.min.js' %}"></script>
diff --git a/salt_observer/views.py b/salt_observer/views.py
index c5b3f47..05ad8bd 100644
--- a/salt_observer/views.py
+++ b/salt_observer/views.py
@@ -158,3 +158,11 @@ class DomainEdit(MarkdownEditMixin, UpdateView, DomainDetail):
 
 class EventView(TemplateView):
     template_name = 'events.html'
+
+    def get_context_data(self, *args, **kwargs):
+        context = super().get_context_data(*args, **kwargs)
+        context.update({
+            'tornado_host': settings.SALT_API['tornado']['host'],
+            'tornado_port': settings.SALT_API['tornado']['port'],
+        })
+        return context
-- 
GitLab