//go:build ignore package main import ( "encoding/json" "flag" "fmt" "log" "os" "os/signal" "time" "golang.org/x/net/websocket" ) func main() { timeout := flag.Int("timeout", 10, "secondes sans message avant de quitter") flag.Parse() args := flag.Args() // Exemples de routes WS disponibles : // ws://localhost:8087/oc/resource/decentralized/all/search/ // ws://localhost:8087/oc/compute/decentralized/all/search/ // ws://localhost:8087/oc/data/decentralized/all/search/ // ws://localhost:8087/oc/processing/decentralized/all/search/ // ws://localhost:8087/oc/storage/decentralized/all/search/ // ws://localhost:8087/oc/workflow/decentralized/all/search/ url := "ws://localhost:8087/oc/resource/decentralized/all/search/demo" token := "" if len(args) >= 1 { url = args[0] } if len(args) >= 2 { token = args[1] } origin := "http://localhost/" config, err := websocket.NewConfig(url, origin) if err != nil { log.Fatalf("Config invalide : %v", err) } if token != "" { config.Header.Set("Authorization", "Bearer "+token) fmt.Printf("Token : %s...\n", token[:min(20, len(token))]) } fmt.Printf("Connexion à : %s\n", url) ws, err := websocket.DialConfig(config) if err != nil { log.Fatalf("Impossible de se connecter : %v", err) } defer ws.Close() fmt.Println("Connecté — en attente de messages...\n") stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) msgs := make(chan string) errs := make(chan error, 1) go func() { for { var raw string if err := websocket.Message.Receive(ws, &raw); err != nil { errs <- err return } msgs <- raw } }() idleTimer := time.NewTimer(time.Duration(*timeout) * time.Second) defer idleTimer.Stop() for { select { case <-stop: fmt.Println("\nInterruption — fermeture.") return case err := <-errs: fmt.Printf("Connexion fermée : %v\n", err) return case <-idleTimer.C: fmt.Printf("Timeout (%ds) — aucun message reçu, fermeture.\n", *timeout) return case raw := <-msgs: idleTimer.Reset(time.Duration(*timeout) * time.Second) var data any if err := json.Unmarshal([]byte(raw), &data); err == nil { b, _ := json.MarshalIndent(data, "", " ") fmt.Println(string(b)) } else { fmt.Printf("Message brut : %s\n", raw) } } } } func min(a, b int) int { if a < b { return a } return b }