This commit is contained in:
mr
2026-03-06 10:13:47 +01:00
parent 29623244c4
commit 98fe2600b3
10 changed files with 445 additions and 29 deletions

115
ws.go Normal file
View File

@@ -0,0 +1,115 @@
//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", 30, "secondes sans message avant de quitter")
flag.Parse()
args := flag.Args()
// Exemples de routes WS disponibles :
// ws://localhost:8090/oc/<workflow-id>/check
// ws://localhost:8090/oc/<workflow-id>/check?as_possible=true
// ws://localhost:8090/oc/<workflow-id>/check?as_possible=true&preemption=true
url := "ws://localhost:8090/oc/WORKFLOW_ID/check?as_possible=true"
token := ""
// Body JSON envoyé comme premier message WebSocket (WorkflowSchedule).
// Seuls start + duration_s sont requis si as_possible=true.
body := `{"start":"` + time.Now().UTC().Format(time.RFC3339) + `","duration_s":3600}`
if len(args) >= 1 {
url = args[0]
}
if len(args) >= 2 {
token = args[1]
}
if len(args) >= 3 {
body = args[2]
}
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é — envoi du body initial...")
// Envoi du WorkflowSchedule comme premier message.
if err := websocket.Message.Send(ws, body); err != nil {
log.Fatalf("Impossible d'envoyer le body initial : %v", err)
}
fmt.Printf("Body envoyé : %s\n\nEn attente de messages...\n\n", body)
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
}