Skip to content

Commit

Permalink
Added a method to reset reconnect state in Centrifugo Client (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom-aviv authored Dec 8, 2024
1 parent f37895c commit 70f479a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
58 changes: 37 additions & 21 deletions Example/SwiftCentrifuge/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -17,7 +17,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="State:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="p1N-9u-SWl">
<rect key="frame" x="16" y="33" width="80" height="21"/>
<rect key="frame" x="16" y="53" width="80" height="21"/>
<constraints>
<constraint firstAttribute="width" constant="80" id="DiD-7r-214"/>
<constraint firstAttribute="height" constant="21" id="S1Z-Ob-WBa"/>
Expand All @@ -27,7 +27,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Disconnected" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dJq-5C-omx">
<rect key="frame" x="106" y="33" width="253" height="20.5"/>
<rect key="frame" x="106" y="53" width="253" height="20.5"/>
<constraints>
<constraint firstAttribute="height" constant="20.5" id="edd-Ao-q1A"/>
</constraints>
Expand All @@ -36,7 +36,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Message:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="JvX-JC-e7I">
<rect key="frame" x="16" y="62" width="80" height="21"/>
<rect key="frame" x="16" y="82" width="80" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="i5Z-9d-Gbr"/>
<constraint firstAttribute="width" constant="80" id="r7P-NM-C3F"/>
Expand All @@ -46,7 +46,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="NnK-BI-God">
<rect key="frame" x="106" y="62" width="253" height="20.5"/>
<rect key="frame" x="106" y="82" width="253" height="20.5"/>
<constraints>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="20.5" id="7B6-bB-jlQ"/>
</constraints>
Expand All @@ -55,15 +55,15 @@
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="New message" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="axm-wb-jAf">
<rect key="frame" x="16" y="119.5" width="287" height="30"/>
<rect key="frame" x="16" y="139.5" width="287" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="Y95-WT-nd8"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ptu-cf-yEG">
<rect key="frame" x="313" y="119" width="46" height="30"/>
<rect key="frame" x="313" y="139" width="46" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="6Oi-Oo-Ans"/>
<constraint firstAttribute="width" constant="46" id="IOX-U8-Swa"/>
Expand All @@ -73,21 +73,38 @@
<action selector="send:" destination="BYZ-38-t0r" eventType="touchUpInside" id="5eR-zY-KM1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UO0-oE-0Vr">
<rect key="frame" x="16" y="165" width="343" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="K8V-Gm-of5"/>
</constraints>
<state key="normal" title="Connect"/>
<connections>
<action selector="connect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fRS-J8-hLD"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="nF0-qX-MRL">
<rect key="frame" x="16" y="185.5" width="343" height="60"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UO0-oE-0Vr">
<rect key="frame" x="0.0" y="0.0" width="343" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="K8V-Gm-of5"/>
</constraints>
<state key="normal" title="Connect"/>
<connections>
<action selector="connect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fRS-J8-hLD"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bCx-Fi-5TE">
<rect key="frame" x="0.0" y="30" width="343" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="bDc-S0-PUy"/>
</constraints>
<state key="normal" title="Reset reconnect state"/>
<connections>
<action selector="resetReconnectState:" destination="BYZ-38-t0r" eventType="touchUpInside" id="hJX-xA-7Na"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="nF0-qX-MRL" firstAttribute="top" secondItem="axm-wb-jAf" secondAttribute="bottom" constant="16" id="0hG-Uj-Cxl"/>
<constraint firstItem="axm-wb-jAf" firstAttribute="top" secondItem="NnK-BI-God" secondAttribute="bottom" constant="37" id="2dX-oO-eM0"/>
<constraint firstItem="nF0-qX-MRL" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="IJJ-hr-g9g"/>
<constraint firstItem="p1N-9u-SWl" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="OjZ-GY-ZuL"/>
<constraint firstItem="JvX-JC-e7I" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="QZb-Uj-sS8"/>
<constraint firstItem="Ptu-cf-yEG" firstAttribute="top" secondItem="NnK-BI-God" secondAttribute="bottom" constant="36.5" id="UCk-ou-RzM"/>
Expand All @@ -96,22 +113,21 @@
<constraint firstItem="p1N-9u-SWl" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="33" id="cf7-J6-8Mi"/>
<constraint firstItem="dJq-5C-omx" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="33" id="cy6-LL-19c"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="dJq-5C-omx" secondAttribute="trailing" constant="16" id="h3Z-oX-NkC"/>
<constraint firstItem="UO0-oE-0Vr" firstAttribute="top" secondItem="axm-wb-jAf" secondAttribute="bottom" constant="15.5" id="hWg-T0-dH2"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="nF0-qX-MRL" secondAttribute="trailing" constant="16" id="iem-93-GRJ"/>
<constraint firstItem="dJq-5C-omx" firstAttribute="leading" secondItem="p1N-9u-SWl" secondAttribute="trailing" constant="10" id="lta-bv-1Jd"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="NnK-BI-God" secondAttribute="trailing" constant="16" id="pNu-Bu-Ugj"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="UO0-oE-0Vr" secondAttribute="trailing" constant="16" id="siN-HI-CMO"/>
<constraint firstItem="Ptu-cf-yEG" firstAttribute="leading" secondItem="axm-wb-jAf" secondAttribute="trailing" constant="10" id="uxj-0d-0lU"/>
<constraint firstItem="NnK-BI-God" firstAttribute="leading" secondItem="JvX-JC-e7I" secondAttribute="trailing" constant="10" id="y0n-WI-cr6"/>
<constraint firstItem="NnK-BI-God" firstAttribute="top" secondItem="dJq-5C-omx" secondAttribute="bottom" constant="8.5" id="z14-cT-gTg"/>
<constraint firstItem="axm-wb-jAf" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="zag-fQ-2Qu"/>
<constraint firstItem="UO0-oE-0Vr" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="16" id="zwT-cN-kvU"/>
</constraints>
</view>
<connections>
<outlet property="clientState" destination="dJq-5C-omx" id="7qf-Tc-lHr"/>
<outlet property="connectButton" destination="UO0-oE-0Vr" id="mJS-2T-8Av"/>
<outlet property="lastMessage" destination="NnK-BI-God" id="fc8-en-aGv"/>
<outlet property="newMessage" destination="axm-wb-jAf" id="lrM-qo-zPW"/>
<outlet property="resetReconnectStateButton" destination="bCx-Fi-5TE" id="0vR-MR-AH2"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
13 changes: 12 additions & 1 deletion Example/SwiftCentrifuge/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ class ViewController: UIViewController {
@IBOutlet weak var lastMessage: UILabel!
@IBOutlet weak var newMessage: UITextField!
@IBOutlet weak var connectButton: UIButton!

@IBOutlet weak var resetReconnectStateButton: UIButton!

private var client: CentrifugeClient?
private var sub: CentrifugeSubscription?

override func viewDidLoad() {
super.viewDidLoad()

resetReconnectStateButton.isHidden = true

NotificationCenter.default.addObserver(self, selector: #selector(self.disconnectClient(_:)), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.connectClient(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)

Expand Down Expand Up @@ -88,6 +92,10 @@ class ViewController: UIViewController {
}
}
}

@IBAction func resetReconnectState(_ sender: Any) {
self.client?.resetReconnectState()
}
}

extension ViewController: CentrifugeConnectionTokenGetter {
Expand All @@ -102,6 +110,7 @@ extension ViewController: CentrifugeClientDelegate {
DispatchQueue.main.async { [weak self] in
self?.clientState.text = "Connected"
self?.connectButton.setTitle("Disconnect", for: .normal)
self?.resetReconnectStateButton.isHidden = true
}
}

Expand All @@ -110,6 +119,7 @@ extension ViewController: CentrifugeClientDelegate {
DispatchQueue.main.async { [weak self] in
self?.clientState.text = "Disconnected"
self?.connectButton.setTitle("Connect", for: .normal)
self?.resetReconnectStateButton.isHidden = true
}
}

Expand All @@ -118,6 +128,7 @@ extension ViewController: CentrifugeClientDelegate {
DispatchQueue.main.async { [weak self] in
self?.clientState.text = "Connecting"
self?.connectButton.setTitle("Disconnect", for: .normal)
self?.resetReconnectStateButton.isHidden = false
}
}

Expand Down
18 changes: 16 additions & 2 deletions Sources/SwiftCentrifuge/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ public class CentrifugeClient {
}
}

/**
Clears the reconnect state, resetting attempts and delays.
Schedules a reconnect immediately if one was pending.
*/
public func resetReconnectState() {
self.syncQueue.async { [weak self] in
guard let strongSelf = self else { return }
strongSelf.reconnectAttempts = 0

guard strongSelf.reconnectTask != nil else { return }
strongSelf.scheduleReconnect(immediately: true)
}
}

/**
setToken allows updating connection token.
- parameter token: String
Expand Down Expand Up @@ -733,12 +747,12 @@ fileprivate extension CentrifugeClient {
}
}

private func scheduleReconnect() {
private func scheduleReconnect(immediately: Bool = false) {
assertIsOnQueue(syncQueue)

guard self.state == .connecting else { return }

let delay = self.getBackoffDelay(
let delay = immediately ? 0.0 : self.getBackoffDelay(
step: self.reconnectAttempts,
minDelay: self.config.minReconnectDelay,
maxDelay: self.config.maxReconnectDelay
Expand Down

0 comments on commit 70f479a

Please sign in to comment.