-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.go
More file actions
52 lines (43 loc) · 1.4 KB
/
example_usage.go
File metadata and controls
52 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package stomp
import (
"context"
"fmt"
"log/slog"
"net/http"
"github.com/gorilla/websocket"
)
// ExampleUsage demonstrates how to use the subscription listener functionality
func ExampleUsage() {
ctx := context.Background()
// Create WebSocket upgrader
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // Allow all origins in this example
},
}
// Create STOMP server
server := NewStompServer(upgrader)
// Add subscription listeners for mission updates
err := server.AddSubscriptionListener("/topic/mission/{missionId}/updates", func(ctx context.Context, topic string, params map[string]string) {
missionId := params["missionId"]
slog.Info("Mission %s subscribed to updates on topic: %s", missionId, topic)
// Start streaming mission updates
startMissionUpdateStreaming(missionId)
})
if err != nil {
slog.Error("Failed to add mission update listener: %v", err)
return
}
// Start HTTP server
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
server.HandleWebSocket(ctx, w, r)
})
slog.Info("STOMP WebSocket server starting on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
slog.Error("Server failed to start: %v", err)
}
}
func startMissionUpdateStreaming(missionId string) {
fmt.Printf("Starting mission update streaming for mission: %s\n", missionId)
// Implement your mission update streaming logic here
}