Quellcode durchsuchen

fix: websocket implementation not working because of the CORS headers

master
Blaz Smehov vor 2 Tagen
Ursprung
Commit
8c14b77ba8
2 geänderte Dateien mit 28 neuen und 8 gelöschten Zeilen
  1. +26
    -8
      cmd/server/main.go
  2. +2
    -0
      scripts/testAPI.sh

+ 26
- 8
cmd/server/main.go Datei anzeigen

@@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os/signal" "os/signal"
"strings"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@@ -71,11 +72,20 @@ func main() {
r.HandleFunc("/api/settings", controller.SettingsListController(appState, client, ctx)).Methods("GET") r.HandleFunc("/api/settings", controller.SettingsListController(appState, client, ctx)).Methods("GET")
r.HandleFunc("/api/settings", controller.SettingsEditController(settingsWriter, appState, client, ctx)).Methods("POST") r.HandleFunc("/api/settings", controller.SettingsEditController(settingsWriter, appState, client, ctx)).Methods("POST")


// r.HandleFunc("/api/beacons/ws", serveWs(appState, ctx))
wsHandler := http.HandlerFunc(serveWs(appState, ctx))
restApiHandler := handlers.CORS(originsOk, headersOk, methodsOk)(r)
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/beacons/ws") {
wsHandler.ServeHTTP(w, r)
return
}

restApiHandler.ServeHTTP(w, r)
})


server := http.Server{ server := http.Server{
Addr: cfg.HTTPAddr, Addr: cfg.HTTPAddr,
Handler: handlers.CORS(originsOk, headersOk, methodsOk)(r),
Handler: mainHandler,
} }


go server.ListenAndServe() go server.ListenAndServe()
@@ -119,9 +129,9 @@ func serveWs(appstate *appcontext.AppState, ctx context.Context) http.HandlerFun
} }
return return
} }
wg.Add(1)
wg.Add(2)
go writer(ws, appstate, ctx) go writer(ws, appstate, ctx)
reader(ws)
go reader(ws, ctx)
} }
} }


@@ -161,17 +171,25 @@ func writer(ws *websocket.Conn, appstate *appcontext.AppState, ctx context.Conte
} }
} }


func reader(ws *websocket.Conn) {
func reader(ws *websocket.Conn, ctx context.Context) {
defer func() { defer func() {
ws.Close() ws.Close()
wg.Done()
}() }()
ws.SetReadLimit(512) ws.SetReadLimit(512)
ws.SetReadDeadline(time.Now().Add((60 * 9) / 10 * time.Second)) ws.SetReadDeadline(time.Now().Add((60 * 9) / 10 * time.Second))
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add((60 * 9) / 10 * time.Second)); return nil }) ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add((60 * 9) / 10 * time.Second)); return nil })
for { for {
_, _, err := ws.ReadMessage()
if err != nil {
break
fmt.Println("this is called")
select {
case <-ctx.Done():
fmt.Println("closing websocket reader")
return
default:
_, _, err := ws.ReadMessage()
if err != nil {
return
}
} }
} }
} }

+ 2
- 0
scripts/testAPI.sh Datei anzeigen

@@ -8,6 +8,8 @@ curl -s -X POST $URL \
-d '{"Beacon_id":"'"$BEACON_ID"'","Name":"Beacon1","tx_power":-59,"rssi":-70}' -d '{"Beacon_id":"'"$BEACON_ID"'","Name":"Beacon1","tx_power":-59,"rssi":-70}'
echo -e "\n" echo -e "\n"


sleep 1

curl -X GET $URL curl -X GET $URL


sleep 1 sleep 1

Laden…
Abbrechen
Speichern