Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/pusher/NWWebSocket/compare/0.5.7...HEAD)
## [Unreleased](https://github.com/pusher/NWWebSocket/compare/0.5.8...HEAD)

## [0.5.8](https://github.com/pusher/NWWebSocket/compare/0.5.7...0.5.8) - 2025-12-05

### Fixed

- Websocket reconnection stuck after network disruption [#59]

## [0.5.7](https://github.com/pusher/NWWebSocket/compare/0.5.6...0.5.7) - 2025-06-19

Expand Down
2 changes: 1 addition & 1 deletion NWWebSocket.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'NWWebSocket'
s.version = '0.5.7'
s.version = '0.5.8'
s.summary = 'A WebSocket client written in Swift, using the Network framework from Apple'
s.homepage = 'https://github.com/pusher/NWWebSocket'
s.license = 'MIT'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '14.0'
use_frameworks!

pod 'NWWebSocket', '~> 0.5.7'
pod 'NWWebSocket', '~> 0.5.8'
```

Then, run the following command:
Expand Down Expand Up @@ -90,7 +90,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/pusher/NWWebSocket.git",
.upToNextMajor(from: "0.5.7")),
.upToNextMajor(from: "0.5.8")),
],
targets: [
.target(
Expand Down
69 changes: 42 additions & 27 deletions Sources/NWWebSocket/Model/Client/NWWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ open class NWWebSocket: WebSocketConnection {
private var disconnectionWorkItem: DispatchWorkItem?
private var isMigratingConnection = false
private var errorWhileWaitingCount = 0
private var isListening = false

// MARK: - Initialization

Expand Down Expand Up @@ -113,37 +114,44 @@ open class NWWebSocket: WebSocketConnection {

/// Connect to the WebSocket.
open func connect() {
if connection == nil {
connection = NWConnection(to: endpoint, using: parameters)
connection?.stateUpdateHandler = { [weak self] state in
self?.stateDidChange(to: state)
}
connection?.betterPathUpdateHandler = { [weak self] isAvailable in
self?.betterPath(isAvailable: isAvailable)
}
connection?.viabilityUpdateHandler = { [weak self] isViable in
self?.viabilityDidChange(isViable: isViable)
}
listen()
connection?.start(queue: connectionQueue)
} else if let conn = connection, !isMigratingConnection {
// Only start if the connection is in a state that allows starting
switch conn.state {
case .setup:
// Connection exists but hasn't been started yet
conn.start(queue: connectionQueue)
case .cancelled, .failed:
// Connection is dead - don't try to start it
// Let the stateDidChange handler deal with cleanup
break
case .ready, .preparing, .waiting:
// Connection is already started or connected - do nothing
break
// If we have an existing connection in a bad state, tear it down first
if let existingConnection = connection {
switch existingConnection.state {
case .cancelled, .failed, .waiting:
// Connection is dead or stuck - tear it down and create fresh
isListening = false
existingConnection.stateUpdateHandler = nil
existingConnection.betterPathUpdateHandler = nil
existingConnection.viabilityUpdateHandler = nil
existingConnection.cancel()
connection = nil
case .ready:
// Already connected - just restart listening if needed
listen()
return
case .setup, .preparing:
// Connection is being set up - let it continue
return
@unknown default:
// Handle unknown states safely
break
}
}

// Create a fresh connection
guard !isMigratingConnection else { return }

connection = NWConnection(to: endpoint, using: parameters)
connection?.stateUpdateHandler = { [weak self] state in
self?.stateDidChange(to: state)
}
connection?.betterPathUpdateHandler = { [weak self] isAvailable in
self?.betterPath(isAvailable: isAvailable)
}
connection?.viabilityUpdateHandler = { [weak self] isViable in
self?.viabilityDidChange(isViable: isViable)
}
listen()
connection?.start(queue: connectionQueue)
}

/// Send a UTF-8 formatted `String` over the WebSocket.
Expand Down Expand Up @@ -171,6 +179,9 @@ open class NWWebSocket: WebSocketConnection {

/// Start listening for messages over the WebSocket.
public func listen() {
guard !isListening else { return }
isListening = true

connection?.receiveMessage { [weak self] (data, context, _, error) in
guard let self = self else {
return
Expand All @@ -181,8 +192,10 @@ open class NWWebSocket: WebSocketConnection {
}

if let error = error {
self.isListening = false
self.reportErrorOrDisconnection(error)
} else {
self.isListening = false
self.listen()
}
}
Expand Down Expand Up @@ -260,6 +273,7 @@ open class NWWebSocket: WebSocketConnection {
switch state {
case .ready:
isMigratingConnection = false
listen() // Restart listening when connection becomes ready
delegate?.webSocketDidConnect(connection: self)
case .waiting(let error):
isMigratingConnection = false
Expand Down Expand Up @@ -463,6 +477,7 @@ open class NWWebSocket: WebSocketConnection {
/// a `cancelled` or `failed` state within the `stateUpdateHandler` closure.
/// - Parameter error: error description
private func tearDownConnection(error: NWError?) {
isListening = false
let connectionToTearDown = connection

// Mark as intentional first
Expand Down
Loading