summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaoran S. Diao <0@hairydiode.xyz>2019-11-22 17:56:36 -0800
committerHaoran S. Diao <0@hairydiode.xyz>2019-11-22 17:56:36 -0800
commit9f9562f26736882701995000a5fe6a4cba8560b5 (patch)
tree77eb8c9742817f9d322851530dc94b4ed93a8fc1
parent9c25d60386002e77a602d16c87d3a7055133f561 (diff)
Editted backend.go to serve timestamp of most recently sent alert, index.htmlHEADmaster
now periodically checks that timestamp to send notifications.
-rw-r--r--backend.go2
-rw-r--r--index.html36
2 files changed, 28 insertions, 10 deletions
diff --git a/backend.go b/backend.go
index 25878c3..aaa4122 100644
--- a/backend.go
+++ b/backend.go
@@ -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");
}
diff --git a/index.html b/index.html
index 49a40b5..723e46e 100644
--- a/index.html
+++ b/index.html
@@ -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>