@@ -52,6 +52,30 @@ class PortForwardViewModel: ObservableObject {
52
52
53
53
init ( webSocketService: WebSocketService = WebSocketService ( ) ) {
54
54
self . webSocketService = webSocketService
55
+
56
+ // Set up notification observer for client disconnection
57
+ NotificationCenter . default. addObserver (
58
+ self ,
59
+ selector: #selector( handleClientDisconnected ( _: ) ) ,
60
+ name: . clientDisconnected,
61
+ object: nil
62
+ )
63
+ }
64
+
65
+ deinit {
66
+ NotificationCenter . default. removeObserver ( self )
67
+ }
68
+
69
+ @objc private func handleClientDisconnected( _ notification: Notification ) {
70
+ // Extract the client ID from the notification
71
+ guard let clientId = notification. userInfo ? [ " clientId " ] as? String else {
72
+ return
73
+ }
74
+
75
+ // Close all port forwards for this client
76
+ DispatchQueue . main. async { [ weak self] in
77
+ self ? . closeAllPortForwardsForClient ( clientId: clientId)
78
+ }
55
79
}
56
80
57
81
// Helper method for logging errors
@@ -142,6 +166,17 @@ class PortForwardViewModel: ObservableObject {
142
166
objectWillChange. send ( )
143
167
}
144
168
169
+ // Close all port forwards for a specific client
170
+ func closeAllPortForwardsForClient( clientId: String ) {
171
+ // Get all port forward IDs for this client
172
+ let portForwardIds = portForwards. values. filter { $0. clientId == clientId } . map { $0. id }
173
+
174
+ // Close each port forward
175
+ for id in portForwardIds {
176
+ closePortForward ( id: id)
177
+ }
178
+ }
179
+
145
180
private func startLocalServer( for portForward: PortForward ) {
146
181
// Create a TCP listener on localhost with the specified port
147
182
let port = NWEndpoint . Port ( integerLiteral: UInt16 ( portForward. localPort) )
0 commit comments