doing the same thing as before but now rendering the

status using a template.
This commit is contained in:
Erik Brakkee 2024-07-31 19:52:01 +02:00
parent f0dd810541
commit 885b7790d7
4 changed files with 23 additions and 56 deletions

View File

@ -1,46 +0,0 @@
package main
import (
"log"
"net"
"net/http"
"strconv"
"time"
)
type Message struct {
Type string `json:"type"`
Content string `json:"content"`
}
func sessionHandler(w http.ResponseWriter, r *http.Request, conn net.Conn) {
log.Println("Got sessions websocket connection")
go func() {
for {
b := make([]byte, 1024)
log.Printf("Reading from %v", conn)
_, err := conn.Read(b)
if err != nil {
return
}
}
}()
i := 0
for {
time.Sleep(1 * time.Second)
message := `
<div id="mycontent">
New data: ` + strconv.Itoa(i) + `
</div>
`
_, err := conn.Write([]byte(message))
if err == nil {
_, err = conn.Write([]byte("\n"))
}
if err != nil {
log.Printf("ERROR sending message: %v", err)
return
}
i++
}
}

View File

@ -40,4 +40,6 @@ func main() {
render(dir, "index.html", templates2.AboutTab) render(dir, "index.html", templates2.AboutTab)
render(dir, "usage.html", usage) render(dir, "usage.html", usage)
render(dir, "downloads.html", templates2.Downloads) render(dir, "downloads.html", templates2.Downloads)
render(dir, "sessions.html", templates2.SessionsTab)
} }

View File

@ -1,7 +1,9 @@
package converge package converge
import ( import (
"context"
"converge/pkg/models" "converge/pkg/models"
"converge/pkg/server/templates"
"log" "log"
"net" "net"
"sync" "sync"
@ -65,12 +67,7 @@ func (session *WebSession) WriteNotifications() {
log.Println("channel closed") log.Println("channel closed")
} }
log.Println("Got notification: ", notification.Ascii) log.Println("Got notification: ", notification.Ascii)
msg := ` err := templates.State(notification).Render(context.Background(), session.conn)
<div id="mycontent">
<p>V1 ascii-art to be improved </p>
<pre>` + notification.Ascii + `</pre>
</div>`
_, err := session.conn.Write([]byte(msg))
if err != nil { if err != nil {
log.Printf("WS connection closed: %v", err) log.Printf("WS connection closed: %v", err)
return return

View File

@ -1,20 +1,34 @@
package templates package templates
import "converge/pkg/models"
templ Sessions() {
templ Sessions(state *models.State) {
<div hx-ext="ws" ws-connect="/ws/sessions"> <div hx-ext="ws" ws-connect="/ws/sessions">
<h1>sessions</h1> <h1>sessions</h1>
<div id="mycontent"> <div id="status">
Loading... if state != nil {
@State(state)
} else {
"Loading..."
}
</div> </div>
</div> </div>
} }
templ State(state *models.State) {
<div id="status">
<pre>
{ state.Ascii }
</pre>
</div>
}
templ SessionsTab() { templ SessionsTab() {
@BasePage(4) { @BasePage(4) {
@Sessions() @Sessions(nil)
} }
} }