Skip to content

Commit 0417706

Browse files
committed
Update closeConnection function
- Invalidate ping timer and update isConnected property.
1 parent 37523b1 commit 0417706

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Sources/ObservableWebSocketClient/Interface/ObservableWebSocketClient+Connection.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ public extension ObservableWebSocketClient {
1111

1212
/// Sends a close frame with an optional close reason.
1313
///
14+
/// This function also invalidates the `pingTimerInterval`, if any.
15+
///
1416
/// - Parameter closeCode: A `URLSessionWebSocketTask.CloseCode`
1517
/// that indicates the reason for closing the connection. The default is `.normalClosure`.
1618
/// - Parameter reason: Optional further information to explain the closing.
1719
/// The value of this parameter is defined by the endpoints, not by the standard.
1820
func closeConnection(with closeCode: URLSessionWebSocketTask.CloseCode = .normalClosure,
1921
reason: String? = nil) {
22+
pingTimer?.invalidate()
2023
service.close(reason: reason)
24+
isConnected = false
2125
}
2226
}

Sources/ObservableWebSocketClient/Interface/ObservableWebSocketClient.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class ObservableWebSocketClient: Identifiable, Equatable, Codable,
2626

2727
public let websocketURL: URL
2828

29-
private var pingTimer: Timer?
29+
private(set) var pingTimer: Timer?
3030
private let pingTimerInterval: TimeInterval?
3131
private let pingMessage: String?
3232
private let pingMessageWithGeneratedId: ((String) -> String)?
@@ -93,8 +93,7 @@ public final class ObservableWebSocketClient: Identifiable, Equatable, Codable,
9393
}
9494

9595
deinit {
96-
pingTimer?.invalidate()
97-
service.close()
96+
closeConnection()
9897
}
9998
}
10099

0 commit comments

Comments
 (0)