Phase 5a: persistent websocket with request ids and reconnect
continuous-integration/drone/push Build encountered an error

Client (ts/network_ts.ts): one WebSocket per page. Commands queue and go
out one at a time with a client-chosen id; the response is matched on the
echoed id (or to the in-flight request for older servers). 12 s timeout
per request, exponential-backoff reconnect (0.5 s to 10 s), the in-flight
request is retried after a reconnect, and log polls are de-duplicated so
they cannot pile up behind a stalled connection. The old global flag that
silently dropped any request made while another was in flight is gone.

Server (src/webserver.go): the /data/ handler now serves any number of
commands on one connection (it used to break out of its loop after the
first reply without closing the socket, leaving it open and deaf; the old
client papered over that by opening a new socket per request). Connection
closed on exit, request id echoed in the response.

Test: TestWSServesMultipleCommandsPerConnection.
This commit is contained in:
2026-09-26 13:19:38 +10:00
parent cbc5bc5da2
commit 3a3ba861dc
8 changed files with 475 additions and 242 deletions
+6
View File
@@ -5,6 +5,10 @@ type RequestStruct struct {
// Befehle an xTeVe
Cmd string `json:"cmd"`
// Client-chosen request id, echoed in the response so the web UI can
// match answers on a long-lived connection.
ID string `json:"id,omitempty"`
// Benutzer
DeleteUser bool `json:"deleteUser,omitempty"`
UserData map[string]any `json:"userData,omitempty"`
@@ -73,6 +77,8 @@ type RequestStruct struct {
// ResponseStruct : Antworten an den Client (WEB)
type ResponseStruct struct {
ID string `json:"id,omitempty"`
ClientInfo struct {
ARCH string `json:"arch"`
Branch string `json:"branch,omitempty"`
+7 -11
View File
@@ -330,22 +330,18 @@ func WS(w http.ResponseWriter, r *http.Request) {
var newToken string
/*
if r.Header.Get("Origin") != "http://"+r.Host {
httpStatusError(w, r, 403)
return
}
*/
// Upgrade writes its own error response (e.g. 403 for a bad Origin).
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
ShowError(err, 0)
return
}
defer conn.Close()
setGlobalDomain(r.Host)
// One connection serves any number of commands, one at a time, until the
// client goes away or a write fails.
for {
// Fresh structs per command: a failed command must not leak its
@@ -360,6 +356,8 @@ func WS(w http.ResponseWriter, r *http.Request) {
return
}
response.ID = request.ID
if !System.ConfigurationWizard {
switch Settings.AuthenticationWEB {
@@ -398,10 +396,9 @@ func WS(w http.ResponseWriter, r *http.Request) {
response = setDefaultResponseData(response, false)
if err = conn.WriteJSON(response); err != nil {
ShowError(err, 1022)
} else {
return
}
return
continue
case "loadFiles":
//response.Response = Settings.Files
@@ -572,8 +569,7 @@ func WS(w http.ResponseWriter, r *http.Request) {
if err = conn.WriteJSON(response); err != nil {
ShowError(err, 1022)
} else {
break
return
}
}
+31
View File
@@ -5,6 +5,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
@@ -137,3 +138,33 @@ func TestSetCookieTokenFlags(t *testing.T) {
t.Error("logout must clear the cookie (negative MaxAge)")
}
}
// A browser keeps one socket open and sends commands one after another; the
// server must answer each on the same connection and echo the request id.
func TestWSServesMultipleCommandsPerConnection(t *testing.T) {
srv, token := newAuthenticatedWSServer(t)
conn, _, err := websocket.DefaultDialer.Dial(wsURL(srv), http.Header{"Cookie": {"Token=" + token}})
if err != nil {
t.Fatalf("dial: %v", err)
}
defer conn.Close()
for i := 1; i <= 3; i++ {
id := strings.Repeat("x", i)
if err := conn.WriteJSON(map[string]any{"cmd": "noop", "id": id}); err != nil {
t.Fatalf("write %d: %v", i, err)
}
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
var response ResponseStruct
if err := conn.ReadJSON(&response); err != nil {
t.Fatalf("read %d: %v (server stopped answering on the same connection)", i, err)
}
if !response.Status {
t.Fatalf("command %d refused: %s", i, response.Error)
}
if response.ID != id {
t.Fatalf("command %d: id %q not echoed, got %q", i, id, response.ID)
}
}
}