diff options
author | Haoran S. Diao <0@hairydiode.xyz> | 2019-11-22 17:56:36 -0800 |
---|---|---|
committer | Haoran S. Diao <0@hairydiode.xyz> | 2019-11-22 17:56:36 -0800 |
commit | 9f9562f26736882701995000a5fe6a4cba8560b5 (patch) | |
tree | 77eb8c9742817f9d322851530dc94b4ed93a8fc1 | |
parent | 9c25d60386002e77a602d16c87d3a7055133f561 (diff) |
now periodically checks that timestamp to send notifications.
-rw-r--r-- | backend.go | 2 | ||||
-rw-r--r-- | index.html | 36 |
2 files changed, 28 insertions, 10 deletions
@@ -14,5 +14,5 @@ func main() { } func messageHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") - fmt.Fprintf(w, "肏"); + fmt.Fprintf(w, "1574466927"); } @@ -1,8 +1,12 @@ <meta charset="UTF-8"> -<body onload="getNotPerm()"> +<body onload="init()"> <!--- Import jquery--> <script src="jquery-3.3.1.min.js"></script> <script> +//Global unix timestamp of the last time a message was displayed, any messages +//from the backend after this time are displayed and the variable is updated to +//that timestamp. It is initialized as the current time. +lastMessageTime = Date.now(); //function called after permission has been requested function handlePermissions(result) { console.log("Permission "+ result); @@ -25,20 +29,34 @@ function getNotPerm() { Notification.requestPermission(); return; } +//sets up the query timer and gets permissions +function init() { + getNotPerm(); + queryTimer = setInterval(queryBackend, 1000); +} //sends a new message function notify() { msg = new Notification("あなたはチンチンが大好き"); } //performing an https POST on the backend -$.post("https://" + window.location.hostname + ":6969/api/msg", - {ld : 0},// data to submit - function(data, status, jqXHR) { //callback function - if (data == ""){ - return; +function queryBackend() { + $.post("https://" + window.location.hostname + ":6969/api/msg", + {ld : 0},// data to submit + function(data, status, jqXHR) { //callback function + if (data == ""){ + return; + } + console.log(data); + timestamp = parseInt(data); + if (timestamp != NaN) { + if (timestamp > lastMessageTime){ + notify(); + lastMessageTime = timestamp; + } + } } - console.log(data); - } -); + ); +} </script> <!--- Permission display--> <p class="pd">You have not granted notification permissions</p> |