Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/NWWebSocket/Model/Client/NWWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ open class NWWebSocket: WebSocketConnection {
// MARK: - Private properties

private var connection: NWConnection?
private var migratedConnection: NWConnection?
private let endpoint: NWEndpoint
private let parameters: NWParameters
private let connectionQueue: DispatchQueue
Expand Down Expand Up @@ -244,14 +245,17 @@ open class NWWebSocket: WebSocketConnection {
/// or a `NWError` if the migration failed for some reason.
private func migrateConnection(completionHandler: @escaping (Result<WebSocketConnection, NWError>) -> Void) {

let migratedConnection = NWConnection(to: endpoint, using: parameters)
migratedConnection?.cancel()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if migrateConnection was already called and successfully ended, in the next call when you cancelling migratedConnection you cancelling current 'connection'

migratedConnection = nil
migratedConnection = NWConnection(to: endpoint, using: parameters)
migratedConnection.stateUpdateHandler = { [weak self] state in
guard let self = self else {
return
}

switch state {
case .ready:
self.connection?.cancel()
self.connection = nil
migratedConnection.stateUpdateHandler = self.stateDidChange(to:)
migratedConnection.betterPathUpdateHandler = self.betterPath(isAvailable:)
Expand Down