Fix websocket
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user