Skip to content

Fixed issue where start function in HTTPConnection would freeze the UI thread in iOS apps. #308

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

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 33 additions & 29 deletions Sources/SignalRClient/HttpConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,40 @@ public class HttpConnection: Connection {
public func start() {
logger.log(logLevel: .info, message: "Starting connection")

if changeState(from: .initial, to: .connecting) == nil {
logger.log(logLevel: .error, message: "Starting connection failed - invalid state")
// the connection is already in use so the startDispatchGroup should not be touched to not affect it
failOpenWithError(error: SignalRError.invalidState, changeState: false, leaveStartDispatchGroup: false)
return
}

startDispatchGroup.enter()

if options.skipNegotiation {
transport = try! self.transportFactory.createTransport(availableTransports: [
TransportDescription(
transportType: TransportType.webSockets,
transferFormats: [TransferFormat.text, TransferFormat.binary])
])
startTransport(connectionId: nil, connectionToken: nil)
} else {
negotiate(negotiateUrl: createNegotiateUrl(), accessToken: nil) { negotiationResponse in
do {
self.transport = try self.transportFactory.createTransport(
availableTransports: negotiationResponse.availableTransports)
} catch {
self.logger.log(logLevel: .error, message: "Creating transport failed: \(error)")
self.failOpenWithError(error: error, changeState: true)
return
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else { return }

if changeState(from: .initial, to: .connecting) == nil {
logger.log(logLevel: .error, message: "Starting connection failed - invalid state")
// the connection is already in use so the startDispatchGroup should not be touched to not affect it
failOpenWithError(error: SignalRError.invalidState, changeState: false, leaveStartDispatchGroup: false)
return
}

startDispatchGroup.enter()

if options.skipNegotiation {
transport = try! self.transportFactory.createTransport(availableTransports: [
TransportDescription(
transportType: TransportType.webSockets,
transferFormats: [TransferFormat.text, TransferFormat.binary])
])
startTransport(connectionId: nil, connectionToken: nil)
} else {
negotiate(negotiateUrl: createNegotiateUrl(), accessToken: nil) { negotiationResponse in
do {
self.transport = try self.transportFactory.createTransport(
availableTransports: negotiationResponse.availableTransports)
} catch {
self.logger.log(logLevel: .error, message: "Creating transport failed: \(error)")
self.failOpenWithError(error: error, changeState: true)
return
}

self.startTransport(
connectionId: negotiationResponse.connectionId, connectionToken: negotiationResponse.connectionToken
)
}

self.startTransport(
connectionId: negotiationResponse.connectionId, connectionToken: negotiationResponse.connectionToken
)
}
}
}
Expand Down