-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsx.go
53 lines (40 loc) · 1.29 KB
/
wsx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//go:build !tinygo
package hub
import (
"net/http"
"golang.org/x/net/websocket"
)
// wsxHandle handles /wsx requests on an htmx WebSocket
func wsxHandle(w http.ResponseWriter, r *http.Request) {
serv := websocket.Server{Handler: websocket.Handler(wsxServe)}
serv.ServeHTTP(w, r)
}
// wsxServe handles htmx WebSocket connections
func wsxServe(ws *websocket.Conn) {
defer ws.Close()
req := ws.Request()
sessionId := req.URL.Query().Get("session-id")
if !sessionUpdate(sessionId) {
LogError("Invalid session", "id", sessionId)
return
}
sessionConn(sessionId, ws)
// Force a refresh of root full view on successful ws connection, in
// case anything has changed since last connect
pkt := Packet{Dst: root.Id, Path: "/device"}
sessionRoute(sessionId, &pkt)
// We use htmx websockets in one-direction only, from the server to the
// client, and only used for sending HTML snippets back to the client.
//
// Keep the websocket connection open, waiting for receives (which will
// never come, see above). Break on EOF or other error.
for {
var message string
if err := websocket.Message.Receive(ws, &message); err != nil {
//LogError("Can't receive", "err", err)
break
}
LogError("Received unexpected message from client", "msg", message)
}
sessionConn(sessionId, nil)
}