Private
Public Access
1
0

Fix websocket

This commit is contained in:
2025-06-17 00:39:50 -07:00
parent ecf66131e9
commit 2041d3ce63
4 changed files with 80 additions and 14 deletions

View File

@@ -13,7 +13,7 @@ import (
"go.buzzert.net/kordophone-mock/server"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"golang.org/x/net/websocket"
"github.com/gorilla/websocket"
)
type MockHTTPServerConfiguration struct {
@@ -398,19 +398,42 @@ func (m *MockHTTPServer) handleUpdates(w http.ResponseWriter, r *http.Request) {
return
}
s := websocket.Server{Handler: websocket.Handler(m.handleUpdatesWebsocket)}
s.ServeHTTP(w, r)
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error().Err(err).Msg("websocket upgrade failed")
return
}
m.handleUpdatesWebsocket(c)
}
func (m *MockHTTPServer) handleUpdatesWebsocket(c *websocket.Conn) {
// Fetch updates continuously
defer c.Close()
// Start a goroutine to handle incoming messages (pings, usually)
go func() {
for {
_, _, err := c.ReadMessage()
if err != nil {
log.Error().Err(err).Msg("WebSocket read error")
return
}
}
}()
for {
// Fetch updates (blocking)
updates := m.Server.FetchUpdatesBlocking(-1)
// Send updates to client
err := websocket.JSON.Send(c, updates)
err := c.WriteJSON(updates)
if err != nil {
log.Error().Err(err).Msg("handleUpdatesWebsocket: Error sending updates to client (probably disconnected)")
return

View File

@@ -15,7 +15,7 @@ import (
"go.buzzert.net/kordophone-mock/model"
"go.buzzert.net/kordophone-mock/server"
"go.buzzert.net/kordophone-mock/web"
"golang.org/x/net/websocket"
"github.com/gorilla/websocket"
)
func TestVersion(t *testing.T) {
@@ -334,7 +334,7 @@ func TestUpdatesWebsocket(t *testing.T) {
// Open websocket connection
wsURL := "ws" + strings.TrimPrefix(httpServer.URL, "http") + "/updates"
ws, err := websocket.Dial(wsURL, "ws", httpServer.URL)
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
if err != nil {
t.Fatalf("Error opening websocket: %s", err)
}
@@ -345,7 +345,7 @@ func TestUpdatesWebsocket(t *testing.T) {
go func() {
// Read from websocket
var updates []model.UpdateItem
err := websocket.JSON.Receive(ws, &updates)
err := ws.ReadJSON(&updates)
if err != nil {
errorEncountered <- err