Fixed HTML and made format a function instead of template string

master
noah metz 2023-12-30 16:21:46 -07:00
parent 54ca0a96d6
commit 47c93e5141
2 changed files with 18 additions and 14 deletions

@ -16,9 +16,10 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
) )
type MQTTFormatFunc func(mqtt.Message) []byte
type MQTTHandler struct { type MQTTHandler struct {
sync.Mutex sync.Mutex
Template string Format MQTTFormatFunc
Channels []chan mqtt.Message Channels []chan mqtt.Message
} }
@ -87,9 +88,9 @@ func NewMQTTHandlerClient(broker string, username string, password string, id st
}, nil }, nil
} }
func (client *MQTTHandlerClient) NewHandler(subscription string, template string) (*MQTTHandler, error) { func (client *MQTTHandlerClient) NewHandler(subscription string, format MQTTFormatFunc) (*MQTTHandler, error) {
handler := &MQTTHandler{ handler := &MQTTHandler{
Template: template, Format: format,
} }
sub_token := client.Subscribe(subscription, 0x00, handler.ProcessMessage) sub_token := client.Subscribe(subscription, 0x00, handler.ProcessMessage)
@ -103,10 +104,6 @@ func (client *MQTTHandlerClient) NewHandler(subscription string, template string
return handler, nil return handler, nil
} }
func (handler *MQTTHandler) Format(message mqtt.Message) []byte {
return []byte(fmt.Sprintf(handler.Template, message.Payload()))
}
func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel := make(chan mqtt.Message, 1) channel := make(chan mqtt.Message, 1)
remove_channel := handler.AddChannel(channel) remove_channel := handler.AddChannel(channel)
@ -141,8 +138,9 @@ func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
os.Stderr.WriteString("websocket context done") os.Stderr.WriteString("websocket context done")
running = false running = false
case message := <- channel: case message := <- channel:
os.Stderr.WriteString(fmt.Sprintf("websocket write: %s\n", message.Payload())) text := handler.Format(message)
err := conn.Write(ctx, websocket.MessageText, message.Payload()) os.Stderr.WriteString(fmt.Sprintf("websocket write: %s\n", text))
err := conn.Write(ctx, websocket.MessageText, text)
if err != nil { if err != nil {
os.Stderr.WriteString(fmt.Sprintf("websocket write error: %s\n", err)) os.Stderr.WriteString(fmt.Sprintf("websocket write error: %s\n", err))
running = false running = false
@ -151,13 +149,19 @@ func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func PayloadFormatFunc(template string) MQTTFormatFunc {
return func(message mqtt.Message) []byte {
return []byte(fmt.Sprintf(template, message.Payload()))
}
}
func main() { func main() {
handler_client, err := NewMQTTHandlerClient("tcp://localhost:1883", "", "", "htmx") handler_client, err := NewMQTTHandlerClient("tcp://localhost:1883", "", "", "htmx")
if err != nil { if err != nil {
panic(err) panic(err)
} }
handler_1, err := handler_client.NewHandler("test", "%s") handler_1, err := handler_client.NewHandler("test", PayloadFormatFunc(`<p id="test">%s</p>`))
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -1,4 +1,4 @@
<!DOCTYPE <!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>HTMX MQTT Test Page</title> <title>HTMX MQTT Test Page</title>
@ -11,10 +11,10 @@
</head> </head>
<body> <body>
<h1>Test Page</h1> <h1>Test Page</h1>
<!-- Use "<p id=\"messages\">test_0</br></p>" as the websocket message to replace the "messages" div --> <!-- Use "<p id=\"test\">test_0</br></p>" as the websocket message to replace the "test" element -->
<!-- Alternatively do "<p hx-swap-oob=\"beforeend:#messages\">test_0</br></p>" to add it to the end of the messages div --> <!-- Alternatively do "<p hx-swap-oob=\"beforeend:#test\">test_0</br></p>" to add it to the end of the "test" element -->
<div hx-ext="ws" ws-connect="/ws"> <div hx-ext="ws" ws-connect="/ws">
<div id="messages"></div> <p id="test"></p>
</div> </div>
</body> </body>
</html> </html>