Skip to content

Commit

Permalink
WebSocket re-connection improvements
Browse files Browse the repository at this point in the history
- Updates 'NWWebSocket' for connection viability and migration reporting
- Removes 'Reachability' as a dependency
  • Loading branch information
Daniel Browne committed Nov 20, 2020
1 parent 0709a7d commit 67f6e78
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 34 deletions.
3 changes: 1 addition & 2 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
github "ashleymills/Reachability.swift" ~> 5.0.0
github "jedisct1/swift-sodium" == 0.8.0
github "pusher/NWWebSocket" ~> 0.3.0
github "pusher/NWWebSocket" ~> 0.5.0
3 changes: 1 addition & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
github "ashleymills/Reachability.swift" "v5.0.0"
github "jedisct1/swift-sodium" "0.8.0"
github "pusher/NWWebSocket" "0.3.0"
github "pusher/NWWebSocket" "0.5.0"
13 changes: 2 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
"repositoryURL": "https://github.com/pusher/NWWebSocket.git",
"state": {
"branch": null,
"revision": "241b5bf7df4fef930e8085483124e77643f108f0",
"version": "0.3.0"
}
},
{
"package": "Reachability",
"repositoryURL": "https://github.com/ashleymills/Reachability.swift.git",
"state": {
"branch": null,
"revision": "98e968e7b6c1318fb61df23e347bc319761e8acb",
"version": "5.0.0"
"revision": "1e433614ac85aba7928320ecd9279b375dce1333",
"version": "0.5.0"
}
}
]
Expand Down
6 changes: 2 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ let package = Package(
.library(name: "PusherSwift", targets: ["PusherSwift"])
],
dependencies: [
.package(url: "https://github.com/ashleymills/Reachability.swift.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/pusher/NWWebSocket.git", .upToNextMajor(from: "0.3.0")),
.package(url: "https://github.com/pusher/NWWebSocket.git", .upToNextMajor(from: "0.5.0")),
],
targets: [
.target(
name: "PusherSwift",
dependencies: [
"Reachability",
"NWWebSocket",
],
path: "Sources",
Expand All @@ -26,7 +24,7 @@ let package = Package(
name: "PusherSwiftTests",
dependencies: ["PusherSwift"],
path: "Tests",
exclude: ["PusherSwiftWithEncryption-Only"]
exclude: ["Unit/PusherSwiftWithEncryption-Only"]
)
],
swiftLanguageVersions: [.v5]
Expand Down
3 changes: 1 addition & 2 deletions PusherSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Pod::Spec.new do |s|
s.source_files = ['Sources/**/*.swift']
s.exclude_files = ['Sources/PusherSwiftWithEncryption-Only/**/*.swift']

s.dependency 'ReachabilitySwift', '~> 5.0'
s.dependency 'NWWebSocket', '~> 0.3.0'
s.dependency 'NWWebSocket', '~> 0.5.0'

s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'
Expand Down
3 changes: 1 addition & 2 deletions PusherSwiftWithEncryption.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ Pod::Spec.new do |s|
s.source_files = ['Sources/**/*.swift']
s.exclude_files = ['Sources/PusherSwift-Only/**/*.swift']

s.dependency 'ReachabilitySwift', '~> 5.0'
s.dependency 'Sodium', '0.8.0'
s.dependency 'NWWebSocket', '~> 0.3.0'
s.dependency 'NWWebSocket', '~> 0.5.0'

s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'
Expand Down
27 changes: 22 additions & 5 deletions Sources/Extensions/PusherWebsocketDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,28 @@ extension PusherConnection: WebSocketConnectionDelegate {
return
}

if let reachability = self.reachability, reachability.connection == .unavailable {
self.delegate?.debugLog?(message: PusherLogger.debug(for: .reconnectionFailureLikely))
attemptReconnect(closeCode: closeCode)
}

public func webSocketViabilityDidChange(connection: WebSocketConnection, isViable: Bool) {
if isViable {
self.delegate?.debugLog?(message: PusherLogger.debug(for: .networkConnectionViable))
} else {
self.delegate?.debugLog?(message: PusherLogger.debug(for: .networkConnectionUnviable))
}
}

attemptReconnect(closeCode: closeCode)
public func webSocketDidAttemptBetterPathMigration(result: Result<WebSocketConnection, NWError>) {
switch result {
case .success(_):
updateConnectionState(to: .reconnecting)
case .failure(let error):
self.delegate?.debugLog?(message: PusherLogger.debug(for: .errorReceived,
context: """
Path migration error: \(error.debugDescription)
"""))
break
}
}

/**
Expand Down Expand Up @@ -215,10 +232,10 @@ extension PusherConnection: WebSocketConnectionDelegate {
//
}

public func webSocketDidReceiveError(connection: WebSocketConnection, error: Error) {
public func webSocketDidReceiveError(connection: WebSocketConnection, error: NWError) {
self.delegate?.debugLog?(message: PusherLogger.debug(for: .errorReceived,
context: """
Error (code: \((error as NSError).code)): \(error.localizedDescription)
Error: \(error.debugDescription)
"""))
}
}
8 changes: 2 additions & 6 deletions Sources/Helpers/PusherLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@ internal class PusherLogger {

// Network

case networkReachable = "Network reachable"
case networkUnreachable = "Network unreachable"
case networkConnectionViable = "Network connection became viable"
case networkConnectionUnviable = "Network connection became unviable"

// Websockets

// swiftlint:disable:next identifier_name
case attemptReconnectionAfterReachabilityChange =
"Connection state is 'connected' but received network reachability change so going to call attemptReconnect"
case attemptReconnectionAfterWaiting = "Attempting to reconnect after waiting"
case attemptReconnectionImmediately = "Attempting to reconnect immediately"
case connectionEstablished = "Socket established with socket ID:"
case disconnectionWithoutError = "Websocket is disconnected but no error received"
case errorReceived = "Websocket received error."
case intentionalDisconnection = "Deliberate disconnection - skipping reconnect attempts"
case maxReconnectAttemptsLimitReached = "Max reconnect attempts reached"
case reconnectionFailureLikely = "Network unreachable so reconnect likely to fail"
case pingSent = "Ping sent"
case pongReceived = "Websocket received pong"
case receivedMessage = "websocketDidReceiveMessage"
Expand Down

0 comments on commit 67f6e78

Please sign in to comment.