doing the same thing as before but now rendering the
status using a template.
This commit is contained in:
parent
882f97fa17
commit
638dffd143
@ -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++
|
||||
}
|
||||
}
|
@ -40,4 +40,6 @@ func main() {
|
||||
render(dir, "index.html", templates2.AboutTab)
|
||||
render(dir, "usage.html", usage)
|
||||
render(dir, "downloads.html", templates2.Downloads)
|
||||
|
||||
render(dir, "sessions.html", templates2.SessionsTab)
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package converge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"converge/pkg/models"
|
||||
"converge/pkg/server/templates"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
@ -65,12 +67,7 @@ func (session *WebSession) WriteNotifications() {
|
||||
log.Println("channel closed")
|
||||
}
|
||||
log.Println("Got notification: ", notification.Ascii)
|
||||
msg := `
|
||||
<div id="mycontent">
|
||||
<p>V1 ascii-art to be improved </p>
|
||||
<pre>` + notification.Ascii + `</pre>
|
||||
</div>`
|
||||
_, err := session.conn.Write([]byte(msg))
|
||||
err := templates.State(notification).Render(context.Background(), session.conn)
|
||||
if err != nil {
|
||||
log.Printf("WS connection closed: %v", err)
|
||||
return
|
||||
|
@ -1,20 +1,34 @@
|
||||
package templates
|
||||
|
||||
import "converge/pkg/models"
|
||||
|
||||
templ Sessions() {
|
||||
|
||||
templ Sessions(state *models.State) {
|
||||
<div hx-ext="ws" ws-connect="/ws/sessions">
|
||||
<h1>sessions</h1>
|
||||
|
||||
<div id="mycontent">
|
||||
Loading...
|
||||
<div id="status">
|
||||
if state != nil {
|
||||
@State(state)
|
||||
} else {
|
||||
"Loading..."
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
}
|
||||
|
||||
templ State(state *models.State) {
|
||||
<div id="status">
|
||||
<pre>
|
||||
{ state.Ascii }
|
||||
</pre>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
templ SessionsTab() {
|
||||
@BasePage(4) {
|
||||
@Sessions()
|
||||
@Sessions(nil)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user