Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NWWebSocket.swift #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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