Use secure WebSockets when connecting over HTTPS
authorThierry Florac <thierry.florac@onf.fr>
Thu, 08 Nov 2018 10:48:38 +0100
changeset 39 c9faf9e5a2d8
parent 38 16b093341bf3
child 40 4b36d3cc3d5e
Use secure WebSockets when connecting over HTTPS
src/pyams_notify/skin/resources/js/pyams_notify.js
src/pyams_notify/skin/resources/js/pyams_notify.min.js
--- a/src/pyams_notify/skin/resources/js/pyams_notify.js	Thu Jun 28 08:40:48 2018 +0200
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.js	Thu Nov 08 10:48:38 2018 +0100
@@ -11,7 +11,11 @@
 		initConnection: function() {
 			var tcp_handler = $('[data-ams-notify-server]', '#user-activity').data('ams-notify-server');
 			if (tcp_handler) {
-				var ws = $.WebSocket('ws://' + tcp_handler + '/subscribe');
+				var protocol = 'ws';
+				if (window.location.protocol === 'https') {
+					protocol += 's';
+				}
+				var ws = $.WebSocket(protocol + '://' + tcp_handler + '/subscribe');
 				ws.onopen = PyAMS_notify.onSocketOpened;
 				ws.onmessage = PyAMS_notify.onSocketMessage;
 				ws.onerror = PyAMS_notify.onSocketError;
--- a/src/pyams_notify/skin/resources/js/pyams_notify.min.js	Thu Jun 28 08:40:48 2018 +0200
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.min.js	Thu Nov 08 10:48:38 2018 +0100
@@ -1,1 +1,1 @@
-(function(c,b){var d=b.MyAMS;var a={connection:null,initConnection:function(){var f=c("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(f){var e=c.WebSocket("ws://"+f+"/subscribe");e.onopen=a.onSocketOpened;e.onmessage=a.onSocketMessage;e.onerror=a.onSocketError;e.onclose=a.onSocketClosed;a.connection=e;setInterval(a.checkConnection,30000)}},checkConnection:function(){if((a.connection===null)||(a.connection.readyState===WebSocket.CLOSED)){a.initConnection()}},onSocketOpened:function(e){if(b.console){b.console.debug("WS notifications connection opened")}d.ajax.post("get-notifications-context.json",{},function(f){if(f.principal.id!==""){a.connection.send(JSON.stringify({action:"subscribe",principal:f.principal,context:f.context}));d.ajax.post("get-user-notifications.json",{},a.showNotifications)}})},onSocketMessage:function(e){var f=JSON.parse(e.data);a.notifyOnDesktop(f);a.notifyInWebpage(f)},onSocketError:function(e){if(b.console){b.console.log(e)}},onSocketClosed:function(e){a.connection=null;if(b.console){b.console.debug("WS connection closed")}},notifyOnDesktop:function(e){function f(){var g={title:e.title,body:e.message,icon:e.source.avatar};var h=new Notification(g.title,g);h.onclick=function(){if(e.url){window.open(e.url)}}}if(window.Notification&&(Notification.permission!=="denied")){if(Notification.permission==="default"){Notification.requestPermission(function(g){if(g==="granted"){f()}})}else{f()}}},createNotification:function(h){var e=c("<li></li>");var f=c("<span></span>");var g=c("<a></a>").addClass("msg").attr("href",h.url);if(h.source.avatar){c("<img>").addClass("air air-top-left margin-top-2").attr("src",h.source.avatar).appendTo(g)}else{c("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(g)}c("<time></time>").text(h.timestamp).appendTo(g);c("<span></span>").addClass("from").text(h.source.title).appendTo(g);c("<span></span>").addClass("msg-body").text(h.message).appendTo(g);g.appendTo(f);f.appendTo(e);return e},notifyInWebpage:function(h){var e=c(".badge","#user-activity >span");var g=parseInt(e.text());e.text(g+1);var f=c(".notification-body","#user-activity");a.createNotification(h).prependTo(f);d.skin.checkNotification()},showNotifications:function(h){var j=h.timestamp,l=h.notifications;var k=c(".badge","#user-activity >span");var m=c(".notification-body","#user-activity");var n=c("li",m).length;m.empty();if(l.length>0){m.prev("p").hide();for(var i=0;i<l.length;i++){var e=l[i];a.createNotification(e).appendTo(m)}var f=c("li",m).length;k.text(f-n)}else{m.prev("p").show();k.text("")}var g=c("#activity-update");g.text(g.data("ams-base-label").replace(/\{0\}/,j));d.skin.checkNotification()},refreshNotifications:function(){return function(){d.ajax.post("get-user-notifications.json",{},a.showNotifications)}}};b.PyAMS_notify=a;d.ajax.check(c.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+d.devext+".js",function(){a.initConnection()})})(jQuery,this);
\ No newline at end of file
+!function(t,n){"use strict";var o=n.MyAMS,i={connection:null,initConnection:function(){var n=t("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(n){var o="ws";"https"===window.location.protocol&&(o+="s");var e=t.WebSocket(o+"://"+n+"/subscribe");e.onopen=i.onSocketOpened,e.onmessage=i.onSocketMessage,e.onerror=i.onSocketError,e.onclose=i.onSocketClosed,i.connection=e,setInterval(i.checkConnection,3e4)}},checkConnection:function(){null!==i.connection&&i.connection.readyState!==WebSocket.CLOSED||i.initConnection()},onSocketOpened:function(t){n.console&&n.console.debug("WS notifications connection opened"),o.ajax.post("get-notifications-context.json",{},function(t){""!==t.principal.id&&(i.connection.send(JSON.stringify({action:"subscribe",principal:t.principal,context:t.context})),o.ajax.post("get-user-notifications.json",{},i.showNotifications))})},onSocketMessage:function(t){var n=JSON.parse(t.data);i.notifyOnDesktop(n),i.notifyInWebpage(n)},onSocketError:function(t){n.console&&n.console.log(t)},onSocketClosed:function(t){i.connection=null,n.console&&n.console.debug("WS connection closed")},notifyOnDesktop:function(t){function n(){var n={title:t.title,body:t.message,icon:t.source.avatar};new Notification(n.title,n).onclick=function(){t.url&&window.open(t.url)}}window.Notification&&"denied"!==Notification.permission&&("default"===Notification.permission?Notification.requestPermission(function(t){"granted"===t&&n()}):n())},createNotification:function(n){var o=t("<li></li>"),i=t("<span></span>"),e=t("<a></a>").addClass("msg").attr("href",n.url);return n.source.avatar?t("<img>").addClass("air air-top-left margin-top-2").attr("src",n.source.avatar).appendTo(e):t("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(e),t("<time></time>").text(n.timestamp).appendTo(e),t("<span></span>").addClass("from").text(n.source.title).appendTo(e),t("<span></span>").addClass("msg-body").text(n.message).appendTo(e),e.appendTo(i),i.appendTo(o),o},notifyInWebpage:function(n){var e=t(".badge","#user-activity >span"),a=parseInt(e.text());e.text(a+1);var c=t(".notification-body","#user-activity");i.createNotification(n).prependTo(c),o.skin.checkNotification()},showNotifications:function(n){var e=n.timestamp,a=n.notifications,c=t(".badge","#user-activity >span"),s=t(".notification-body","#user-activity"),r=t("li",s).length;if(s.empty(),a.length>0){s.prev("p").hide();for(var p=0;p<a.length;p++){var f=a[p];i.createNotification(f).appendTo(s)}var l=t("li",s).length;c.text(l-r)}else s.prev("p").show(),c.text("");var d=t("#activity-update");d.text(d.data("ams-base-label").replace(/\{0\}/,e)),o.skin.checkNotification()},refreshNotifications:function(){return function(){o.ajax.post("get-user-notifications.json",{},i.showNotifications)}}};n.PyAMS_notify=i,o.ajax.check(t.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+o.devext+".js",function(){i.initConnection()})}(jQuery,this);