Skip to content

Commit

Permalink
Update closeConnection function
Browse files Browse the repository at this point in the history
 - Invalidate ping timer and update isConnected property.
  • Loading branch information
backslash-f committed Apr 18, 2024
1 parent 37523b1 commit 0417706
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ public extension ObservableWebSocketClient {

/// Sends a close frame with an optional close reason.
///
/// This function also invalidates the `pingTimerInterval`, if any.
///
/// - Parameter closeCode: A `URLSessionWebSocketTask.CloseCode`
/// that indicates the reason for closing the connection. The default is `.normalClosure`.
/// - Parameter reason: Optional further information to explain the closing.
/// The value of this parameter is defined by the endpoints, not by the standard.
func closeConnection(with closeCode: URLSessionWebSocketTask.CloseCode = .normalClosure,
reason: String? = nil) {
pingTimer?.invalidate()
service.close(reason: reason)
isConnected = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class ObservableWebSocketClient: Identifiable, Equatable, Codable,

public let websocketURL: URL

private var pingTimer: Timer?
private(set) var pingTimer: Timer?
private let pingTimerInterval: TimeInterval?
private let pingMessage: String?
private let pingMessageWithGeneratedId: ((String) -> String)?
Expand Down Expand Up @@ -93,8 +93,7 @@ public final class ObservableWebSocketClient: Identifiable, Equatable, Codable,
}

deinit {
pingTimer?.invalidate()
service.close()
closeConnection()
}
}

Expand Down

0 comments on commit 0417706

Please sign in to comment.