Skip to content

Commit

Permalink
poc/rapidleech_like_state_update: init
Browse files Browse the repository at this point in the history
Signed-off-by: lucasew <lucas59356@gmail.com>
  • Loading branch information
lucasew committed Mar 19, 2021
1 parent 65002fd commit 12cec2a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions poc/rapidleech_like_state_update/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lucasew/playground/poc/rapidleech_like_state_update

go 1.16
56 changes: 56 additions & 0 deletions poc/rapidleech_like_state_update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"net/http"
"time"
)

func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":42069", nil)
if err != nil {
panic(err)
}
}

const demoHTML = `
<html>
<head>
<title>Teste</title>
</head>
<body>
<script>
function changeCounter(str) {
document.getElementById("counter").textContent = str
}
</script>
<h1 id="counter"></h1>
</body>
</html>
`

func handler(w http.ResponseWriter, r *http.Request) {
defer println("Conexão encerrada")
w.WriteHeader(200)
// w.Header().Add("Connection", "keep-alive")
// w.Header().Add("Keep-Alive", "timeout=5,max=1")
w.Write([]byte(demoHTML))
counter := 0
ticker := time.Tick(time.Second)
for {
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
select {
case <-ticker:
fmt.Fprintf(w, "<script>changeCounter('%s')</script>", fmt.Sprintf("Contador: %d", counter))
counter++
case <-r.Context().Done():
return
}
// if counter == 10 {
// break
// }
}
}
7 changes: 7 additions & 0 deletions poc/rapidleech_like_state_update/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
buildInputs = with pkgs; [
go
gopls
];
}

0 comments on commit 12cec2a

Please sign in to comment.