|
|
@ -3,7 +3,7 @@ package htmxmqtt
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"log/slog"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
@ -31,7 +31,7 @@ func (handler *MQTTHandler) processMessage(client mqtt.Client, message mqtt.Mess
|
|
|
|
case channel <- message:
|
|
|
|
case channel <- message:
|
|
|
|
remaining = append(remaining, channel)
|
|
|
|
remaining = append(remaining, channel)
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
os.Stderr.WriteString("Channel overflow\n")
|
|
|
|
slog.Warn("Channel overflow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -107,7 +107,10 @@ func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := websocket.Accept(w, r, nil)
|
|
|
|
conn, err := websocket.Accept(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
os.Stderr.WriteString(fmt.Sprintf("websocket accept error: %s\n", err))
|
|
|
|
slog.Error("websocket accept error", "error", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
slog.Info("new websocket connection", "addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer conn.CloseNow()
|
|
|
|
defer conn.CloseNow()
|
|
|
@ -117,11 +120,11 @@ func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for true {
|
|
|
|
for true {
|
|
|
|
msg_type, data, err := conn.Read(ctx)
|
|
|
|
msg_type, data, err := conn.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
os.Stderr.WriteString(fmt.Sprintf("websocket error: %s\n", err))
|
|
|
|
slog.Error("websocket error", "error", err)
|
|
|
|
cancel_func()
|
|
|
|
cancel_func()
|
|
|
|
break
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
os.Stderr.WriteString(fmt.Sprintf("websocket data(%s): %s\n", msg_type, string(data)))
|
|
|
|
slog.Debug("websocket data", "type", msg_type, "data", string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(conn, cancel_func)
|
|
|
|
}(conn, cancel_func)
|
|
|
@ -131,18 +134,20 @@ func (handler *MQTTHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for running == true {
|
|
|
|
for running == true {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <- done:
|
|
|
|
case <- done:
|
|
|
|
os.Stderr.WriteString("websocket context done")
|
|
|
|
slog.Debug("websocket context done")
|
|
|
|
running = false
|
|
|
|
running = false
|
|
|
|
case message := <- channel:
|
|
|
|
case message := <- channel:
|
|
|
|
text := handler.format(message)
|
|
|
|
text := handler.format(message)
|
|
|
|
os.Stderr.WriteString(fmt.Sprintf("websocket write: %s\n", text))
|
|
|
|
slog.Debug("websocket write", "data", text)
|
|
|
|
err := conn.Write(ctx, websocket.MessageText, 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))
|
|
|
|
slog.Error("websocket write error", "error", err)
|
|
|
|
running = false
|
|
|
|
running = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
slog.Info("closing websocket", "addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func PayloadFormatFunc(template string) MQTTFormatFunc {
|
|
|
|
func PayloadFormatFunc(template string) MQTTFormatFunc {
|
|
|
|